Add base api class

This commit is contained in:
Pedro Silva 2023-05-09 18:32:17 +01:00
parent 7facf0c9ea
commit 62238611bf
1 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
import { Response } from "node-fetch"
import BudibaseInternalAPIClient from "../BudibaseInternalAPIClient"
export default class BaseAPI {
client: BudibaseInternalAPIClient
constructor(client: BudibaseInternalAPIClient) {
this.client = client
}
async get(url: string): Promise<[Response, any]> {
const [response, json] = await this.client.get(url)
expect(response).toHaveStatusCode(200)
return [response, json]
}
async post(
url: string,
body?: any,
statusCode?: number
): Promise<[Response, any]> {
const [response, json] = await this.client.post(url, { body })
expect(response).toHaveStatusCode(statusCode ? statusCode : 200)
return [response, json]
}
async put(
url: string,
body?: any,
statusCode?: number
): Promise<[Response, any]> {
const [response, json] = await this.client.put(url, { body })
expect(response).toHaveStatusCode(statusCode ? statusCode : 200)
return [response, json]
}
async patch(
url: string,
body?: any,
statusCode?: number
): Promise<[Response, any]> {
const [response, json] = await this.client.patch(url, { body })
expect(response).toHaveStatusCode(statusCode ? statusCode : 200)
return [response, json]
}
async del(url: string, statusCode?: number): Promise<[Response, any]> {
const [response, json] = await this.client.del(url)
expect(response).toHaveStatusCode(statusCode ? statusCode : 200)
return [response, json]
}
}