Rework the API slightly.
This commit is contained in:
parent
3203cc3d72
commit
f1ed7af439
|
@ -1,71 +1,95 @@
|
||||||
import TestConfiguration from "../TestConfiguration"
|
import TestConfiguration from "../TestConfiguration"
|
||||||
import { SuperTest, Test } from "supertest"
|
import { SuperTest, Test } from "supertest"
|
||||||
|
|
||||||
|
type Headers = Record<string, string | string[] | undefined>
|
||||||
|
|
||||||
export interface TestAPIOpts {
|
export interface TestAPIOpts {
|
||||||
headers?: Record<string, string | string[] | undefined>
|
headers?: Headers
|
||||||
status?: number
|
status?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Expectations {
|
||||||
|
status?: number
|
||||||
|
contentType?: string | RegExp
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RequestOpts {
|
||||||
|
headers?: Headers
|
||||||
|
body?: Record<string, any>
|
||||||
|
fields?: Record<string, any>
|
||||||
|
files?: Record<string, any>
|
||||||
|
expectations?: Expectations
|
||||||
|
}
|
||||||
|
|
||||||
export abstract class TestAPI {
|
export abstract class TestAPI {
|
||||||
config: TestConfiguration
|
config: TestConfiguration
|
||||||
request: SuperTest<Test>
|
request: SuperTest<Test>
|
||||||
|
|
||||||
protected constructor(config: TestConfiguration) {
|
constructor(config: TestConfiguration) {
|
||||||
this.config = config
|
this.config = config
|
||||||
this.request = config.request!
|
this.request = config.request!
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _get = async <T>(
|
protected _get = async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
opts: TestAPIOpts = {}
|
expectations?: Expectations
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
return await this._request<T>("get", url, undefined, opts)
|
return await this._request<T>("get", url, { expectations })
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _post = async <T>(
|
protected _post = async <T>(url: string, opts?: RequestOpts): Promise<T> => {
|
||||||
url: string,
|
return await this._request<T>("post", url, opts)
|
||||||
body: Record<string, any>,
|
|
||||||
opts: TestAPIOpts = {}
|
|
||||||
): Promise<T> => {
|
|
||||||
return await this._request<T>("post", url, body, opts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _put = async <T>(
|
protected _put = async <T>(url: string, opts?: RequestOpts): Promise<T> => {
|
||||||
url: string,
|
return await this._request<T>("put", url, opts)
|
||||||
body: Record<string, any>,
|
|
||||||
opts: TestAPIOpts = {}
|
|
||||||
): Promise<T> => {
|
|
||||||
return await this._request<T>("put", url, body, opts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _patch = async <T>(
|
protected _patch = async <T>(url: string, opts?: RequestOpts): Promise<T> => {
|
||||||
url: string,
|
return await this._request<T>("patch", url, opts)
|
||||||
body: Record<string, any>,
|
|
||||||
opts: TestAPIOpts = {}
|
|
||||||
): Promise<T> => {
|
|
||||||
return await this._request<T>("patch", url, body, opts)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _delete = async <T>(
|
protected _delete = async <T>(
|
||||||
url: string,
|
url: string,
|
||||||
body: Record<string, any>,
|
opts?: RequestOpts
|
||||||
opts: TestAPIOpts = {}
|
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
return await this._request<T>("delete", url, body, opts)
|
return await this._request<T>("delete", url, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected _request = async <T>(
|
protected _request = async <T>(
|
||||||
method: "get" | "post" | "put" | "patch" | "delete",
|
method: "get" | "post" | "put" | "patch" | "delete",
|
||||||
url: string,
|
url: string,
|
||||||
body?: Record<string, any>,
|
opts?: RequestOpts
|
||||||
opts: TestAPIOpts = {}
|
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
const { headers = {}, status = 200 } = opts
|
const { headers = {}, body, fields, files, expectations } = opts || {}
|
||||||
const response = await this.request[method](url)
|
const { status = 200, contentType = /json/ } = expectations || {}
|
||||||
.send(body)
|
|
||||||
.set(this.config.defaultHeaders())
|
let request = this.request[method](url).set(this.config.defaultHeaders())
|
||||||
.set(headers)
|
if (headers) {
|
||||||
.expect("Content-Type", /json/)
|
request = request.set(headers)
|
||||||
|
}
|
||||||
|
if (body) {
|
||||||
|
request = request.send(body)
|
||||||
|
}
|
||||||
|
if (fields) {
|
||||||
|
for (const [key, value] of Object.entries(fields)) {
|
||||||
|
request = request.field(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (files) {
|
||||||
|
for (const [key, value] of Object.entries(files)) {
|
||||||
|
request = request.attach(key, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (contentType) {
|
||||||
|
if (contentType instanceof RegExp) {
|
||||||
|
request = request.expect("Content-Type", contentType)
|
||||||
|
} else {
|
||||||
|
request = request.expect("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await request
|
||||||
|
|
||||||
if (response.status !== status) {
|
if (response.status !== status) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
|
|
@ -5,33 +5,35 @@ import {
|
||||||
SaveTableResponse,
|
SaveTableResponse,
|
||||||
Table,
|
Table,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { TestAPI, TestAPIOpts } from "./base"
|
import { Expectations, TestAPI } from "./base"
|
||||||
|
|
||||||
export class TableAPI extends TestAPI {
|
export class TableAPI extends TestAPI {
|
||||||
save = async (
|
save = async (
|
||||||
data: SaveTableRequest,
|
data: SaveTableRequest,
|
||||||
opts?: TestAPIOpts
|
expectations?: Expectations
|
||||||
): Promise<SaveTableResponse> => {
|
): Promise<SaveTableResponse> => {
|
||||||
return await this._post<SaveTableResponse>("/api/tables", data, opts)
|
return await this._post<SaveTableResponse>("/api/tables", {
|
||||||
|
body: data,
|
||||||
|
expectations,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch = async (opts?: TestAPIOpts): Promise<Table[]> => {
|
fetch = async (expectations?: Expectations): Promise<Table[]> => {
|
||||||
return await this._get<Table[]>("/api/tables", opts)
|
return await this._get<Table[]>("/api/tables", expectations)
|
||||||
}
|
}
|
||||||
|
|
||||||
get = async (tableId: string, opts?: TestAPIOpts): Promise<Table> => {
|
get = async (tableId: string, expectations: Expectations): Promise<Table> => {
|
||||||
return await this._get<Table>(`/api/tables/${tableId}`, opts)
|
return await this._get<Table>(`/api/tables/${tableId}`, expectations)
|
||||||
}
|
}
|
||||||
|
|
||||||
migrate = async (
|
migrate = async (
|
||||||
tableId: string,
|
tableId: string,
|
||||||
data: MigrateRequest,
|
data: MigrateRequest,
|
||||||
opts?: TestAPIOpts
|
expectations?: Expectations
|
||||||
): Promise<MigrateResponse> => {
|
): Promise<MigrateResponse> => {
|
||||||
return await this._post<MigrateResponse>(
|
return await this._post<MigrateResponse>(`/api/tables/${tableId}/migrate`, {
|
||||||
`/api/tables/${tableId}/migrate`,
|
body: data,
|
||||||
data,
|
expectations,
|
||||||
opts
|
})
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue