Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GET /requests endpoint 🦅 #97

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ErrorResponse,
CancelRequestResponse,
cancelRequestResponseSchema,
RequestsFilter,
} from './schema';
import {
composesSchema,
Expand Down Expand Up @@ -51,6 +52,16 @@
this.link = new PublicLink(urlSchema.parse(instance));
}

async requests(filter: RequestsFilter): Promise<Request[]>;
async requests(filter: RequestsFilter, strict: boolean): Promise<unknown>;
async requests(filter: RequestsFilter, strict?: boolean): Promise<unknown> {
if (!this.isStrict(strict)) {
return this.link.get('requests', filter);
}

return requestSchema.array().parse(await this.link.get('requests', filter));
}

Check warning on line 63 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L58-L63

Added lines #L58 - L63 were not covered by tests

async newRequest(request: NewRequest): Promise<NewRequestResponse>;
async newRequest(request: NewRequest, strict: boolean): Promise<unknown>;
async newRequest(request: NewRequest, strict?: boolean): Promise<unknown> {
Expand Down
31 changes: 28 additions & 3 deletions src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,39 @@

protected abstract request(config: AxiosRequestConfig): Promise<unknown>;

protected buildURL(path: string): URL {
protected buildURL(path: string): URL;
protected buildURL<P extends Record<string, string>>(
path: string,
searchParams: P
): URL;
protected buildURL<P extends Record<string, string>>(
path: string,
searchParams?: P
): URL {
let url = new URL(`${this.instance.pathname}/${path}`, this.instance);

if (searchParams) {
url.search = new URLSearchParams(searchParams).toString();
}

Check warning on line 46 in src/link.ts

View check run for this annotation

Codecov / codecov/patch

src/link.ts#L45-L46

Added lines #L45 - L46 were not covered by tests

return url;
}

async get(path: string): Promise<unknown> {
async get(path: string): Promise<unknown>;
async get<P extends Record<string, string>>(
path: string,
searchParams: P
): Promise<unknown>;
async get<P extends Record<string, string>>(
path: string,
searchParams?: P
): Promise<unknown> {
const url = searchParams
? this.buildURL(path, searchParams)

Check warning on line 61 in src/link.ts

View check run for this annotation

Codecov / codecov/patch

src/link.ts#L61

Added line #L61 was not covered by tests
: this.buildURL(path);

const config: AxiosRequestConfig = {
url: this.buildURL(path).toString(),
url: url.toString(),
method: 'GET',
};

Expand Down
9 changes: 9 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof requestsFilterSchema>;

export const newRequestSchema = z.object({
api_key: z.string().min(1),
test: testObjectSchema,
Expand Down