From 62238611bfa603a9af5d37c50fa1ded61e47999c Mon Sep 17 00:00:00 2001 From: Pedro Silva Date: Tue, 9 May 2023 18:32:17 +0100 Subject: [PATCH] Add base api class --- qa-core/src/internal-api/api/apis/BaseAPI.ts | 52 ++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 qa-core/src/internal-api/api/apis/BaseAPI.ts diff --git a/qa-core/src/internal-api/api/apis/BaseAPI.ts b/qa-core/src/internal-api/api/apis/BaseAPI.ts new file mode 100644 index 0000000000..6edbc10701 --- /dev/null +++ b/qa-core/src/internal-api/api/apis/BaseAPI.ts @@ -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] + } +}