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