From 6a6d903ba86702b2b2d8863e68dc8cd8656835bc Mon Sep 17 00:00:00 2001 From: Jan Macku Date: Tue, 20 Feb 2024 13:52:34 +0100 Subject: [PATCH 1/2] feat: add support for query parameters in the URL --- src/link.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/link.ts b/src/link.ts index 07cf224..af535e3 100644 --- a/src/link.ts +++ b/src/link.ts @@ -30,8 +30,21 @@ export abstract class TestingFarmLink { protected abstract request(config: AxiosRequestConfig): Promise; - protected buildURL(path: string): URL { + protected buildURL(path: string): URL; + protected buildURL

>( + path: string, + searchParams: P + ): URL; + protected buildURL

>( + path: string, + searchParams?: P + ): URL { let url = new URL(`${this.instance.pathname}/${path}`, this.instance); + + if (searchParams) { + url.search = new URLSearchParams(searchParams).toString(); + } + return url; } From 532621a97590cb6795e186dcf58359798ddd42da Mon Sep 17 00:00:00 2001 From: Jan Macku Date: Tue, 20 Feb 2024 14:20:48 +0100 Subject: [PATCH 2/2] feat: add support for `GET /requests` endpoint --- README.md | 11 +++++++++++ src/index.ts | 11 +++++++++++ src/link.ts | 16 ++++++++++++++-- src/schema.ts | 9 +++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 26202fc..f00f6c4 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,17 @@ const api = new TestingFarmAPI("https://api.dev.testing-farm.io/v0.1"); await api.about(); ``` +### List a Test Requests + +documentation of - [`GET /requests`](https://api.dev.testing-farm.io/redoc#operation/get_test_requests_v0_1_requests_get) + +```typescript +const queryParams = { /* https://api.dev.testing-farm.io/redoc#operation/get_test_requests_v0_1_requests_get */ } + +const requests: Requests[] = await api.requests(queryParams); +const requests: unknown = await api.requests(queryParams, false); +``` + ### Request a New Test documentation of - [`POST /requests`](https://api.dev.testing-farm.io/redoc#operation/request_a_new_test_v0_1_requests_post) diff --git a/src/index.ts b/src/index.ts index 7904a54..6e43893 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { ErrorResponse, CancelRequestResponse, cancelRequestResponseSchema, + RequestsFilter, } from './schema'; import { composesSchema, @@ -51,6 +52,16 @@ export default class TestingFarmAPI { this.link = new PublicLink(urlSchema.parse(instance)); } + async requests(filter: RequestsFilter): Promise; + async requests(filter: RequestsFilter, strict: boolean): Promise; + async requests(filter: RequestsFilter, strict?: boolean): Promise { + if (!this.isStrict(strict)) { + return this.link.get('requests', filter); + } + + return requestSchema.array().parse(await this.link.get('requests', filter)); + } + async newRequest(request: NewRequest): Promise; async newRequest(request: NewRequest, strict: boolean): Promise; async newRequest(request: NewRequest, strict?: boolean): Promise { diff --git a/src/link.ts b/src/link.ts index af535e3..218ee48 100644 --- a/src/link.ts +++ b/src/link.ts @@ -48,9 +48,21 @@ export abstract class TestingFarmLink { return url; } - async get(path: string): Promise { + async get(path: string): Promise; + async get

>( + path: string, + searchParams: P + ): Promise; + async get

>( + path: string, + searchParams?: P + ): Promise { + const url = searchParams + ? this.buildURL(path, searchParams) + : this.buildURL(path); + const config: AxiosRequestConfig = { - url: this.buildURL(path).toString(), + url: url.toString(), method: 'GET', }; diff --git a/src/schema.ts b/src/schema.ts index 7e54e48..6ab211a 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -113,6 +113,15 @@ const settingsSchema = z.object({ .optional(), }); +export const requestsFilterSchema = z.object({ + state: z.string(), + user_id: z.string(), + created_before: z.string(), + created_after: z.string(), +}); + +export type RequestsFilter = z.infer; + export const newRequestSchema = z.object({ api_key: z.string().min(1), test: testObjectSchema,