Skip to content

Commit

Permalink
wip: cast object to map
Browse files Browse the repository at this point in the history
  • Loading branch information
jamacku committed Mar 11, 2023
1 parent ad397de commit 17d348e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
2 changes: 2 additions & 0 deletions itest/test/attachments.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ test('Create attachment for multiple bugs at once', async () => {
});

test('Get single attachment', async () => {
await expect(api.getAttachment(1)).resolves.toMatchInlineSnapshot();

await expect(api.getAttachment(1)).resolves.toEqual({
bug_id: bugs[0],
content_type: 'image/png',
Expand Down
68 changes: 38 additions & 30 deletions src/link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { URLSearchParams, URL } from 'url';
import { z } from 'zod';
import { z, ZodSchema } from 'zod';

import axios, { AxiosRequestConfig } from 'axios';
import { loginResponseSchema } from './types';
Expand Down Expand Up @@ -28,10 +28,10 @@ function isError(payload: unknown): payload is ApiError {
return payload && typeof payload == 'object' && payload.error;
}

async function performRequest<TValues>(
config: AxiosRequestConfig,
schema: z.Schema<TValues>,
): Promise<TValues> {
async function performRequest<
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(config: AxiosRequestConfig, schema: TSchema): Promise<KValues> {
try {
let response = await axios.request({
...config,
Expand Down Expand Up @@ -67,10 +67,10 @@ export abstract class BugzillaLink {
this.instance = new URL('rest/', instance);
}

protected abstract request<TValues>(
config: AxiosRequestConfig,
schema: z.Schema<TValues>,
): Promise<TValues>;
protected abstract request<
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(config: AxiosRequestConfig, schema: TSchema): Promise<KValues>;

protected buildURL(path: string, query?: SearchParams): URL {
let url = new URL(path, this.instance);
Expand All @@ -80,11 +80,11 @@ export abstract class BugzillaLink {
return url;
}

public async get<TValues>(
public async get<TSchema extends ZodSchema, KValues extends z.infer<TSchema>>(
path: string,
schema: z.Schema<TValues>,
schema: TSchema,
searchParams?: SearchParams,
): Promise<TValues> {
): Promise<KValues> {
return this.request(
{
url: this.buildURL(path, searchParams).toString(),
Expand All @@ -93,12 +93,16 @@ export abstract class BugzillaLink {
);
}

public async post<R, TValues>(
public async post<
R,
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(
path: string,
schema: z.Schema<TValues>,
schema: TSchema,
content: R,
searchParams?: SearchParams,
): Promise<TValues> {
): Promise<KValues> {
return this.request(
{
url: this.buildURL(path, searchParams).toString(),
Expand All @@ -112,12 +116,16 @@ export abstract class BugzillaLink {
);
}

public async put<R, TValues>(
public async put<
R,
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(
path: string,
schema: z.Schema<TValues>,
schema: TSchema,
content: R,
searchParams?: SearchParams,
): Promise<TValues> {
): Promise<KValues> {
return this.request(
{
url: this.buildURL(path, searchParams).toString(),
Expand All @@ -133,10 +141,10 @@ export abstract class BugzillaLink {
}

export class PublicLink extends BugzillaLink {
protected async request<TValues>(
config: AxiosRequestConfig,
schema: z.Schema<TValues>,
): Promise<TValues> {
protected async request<
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(config: AxiosRequestConfig, schema: TSchema): Promise<KValues> {
return performRequest(config, schema);
}
}
Expand All @@ -149,10 +157,10 @@ export class ApiKeyLink extends BugzillaLink {
super(instance);
}

protected async request<TValues>(
config: AxiosRequestConfig,
schema: z.Schema<TValues>,
): Promise<TValues> {
protected async request<
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(config: AxiosRequestConfig, schema: TSchema): Promise<KValues> {
return performRequest(
{
...config,
Expand Down Expand Up @@ -198,10 +206,10 @@ export class PasswordLink extends BugzillaLink {
return loginInfo.token;
}

protected async request<TValues>(
config: AxiosRequestConfig,
schema: z.Schema<TValues>,
): Promise<TValues> {
protected async request<
TSchema extends ZodSchema,
KValues extends z.infer<TSchema>,
>(config: AxiosRequestConfig, schema: TSchema): Promise<KValues> {
if (!this.token) {
this.token = await this.login();
}
Expand Down
34 changes: 27 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,29 @@ export const commentsTemplateSchema = z.object({

export type CommentsTemplate = z.infer<typeof commentsTemplateSchema>;

function castObjectToMap<K extends z.ZodTypeAny, V extends z.ZodTypeAny>(
keyValidator: K,
valueValidator: V,
) {
return z.record(keyValidator, valueValidator).transform(record => {
let result = new Map<z.infer<K>, z.infer<V>>();

for (let [k, v] of Object.entries(record)) {
// if (typeof k === 'number') {
// result.set(k, v);
// }
if (k?.toString()) {
result.set(k.toString(), v);
}
}

return result;
});
}

export const commentsSchema = z.object({
bugs: z.map(z.number(), commentsTemplateSchema),
comments: z.map(z.number(), commentSchema),
bugs: castObjectToMap(z.number(), commentsTemplateSchema),
comments: castObjectToMap(z.number(), commentSchema),
});

export type Comments = z.infer<typeof commentsSchema>;
Expand Down Expand Up @@ -222,7 +242,7 @@ export const updateBugContentSchema = z.object({
cc: updateListSchema(z.string()).optional(),
is_cc_accessible: z.boolean().optional(),
comment: createCommentContentSchema.optional(),
comment_is_private: z.map(z.number(), z.boolean()).optional(),
comment_is_private: castObjectToMap(z.number(), z.boolean()).optional(),
comment_tags: z.array(z.string()).optional(),
component: z.string().optional(),
deadline: z.string().datetime().optional(),
Expand Down Expand Up @@ -262,7 +282,7 @@ export const updatedBugSchema = z.object({
id: z.number(),
alias: z.array(z.string()),
last_change_time: z.string().datetime(),
changes: z.map(z.string(), changesSchema),
changes: castObjectToMap(z.string(), changesSchema),
});

export type UpdatedBug = z.infer<typeof updatedBugSchema>;
Expand Down Expand Up @@ -294,8 +314,8 @@ export const attachmentSchema = z.object({
export type Attachment = z.infer<typeof attachmentSchema>;

export const attachmentsSchema = z.object({
bugs: z.map(z.number(), z.array(attachmentSchema)),
attachments: z.map(z.number(), attachmentSchema),
bugs: castObjectToMap(z.number(), z.array(attachmentSchema)),
attachments: castObjectToMap(z.number(), attachmentSchema),
});

export type Attachments = z.infer<typeof attachmentsSchema>;
Expand Down Expand Up @@ -343,7 +363,7 @@ export type UpdateAttachmentContent = z.infer<
export const updatedAttachmentSchema = z.object({
id: z.number(),
last_change_time: z.string().datetime(),
changes: z.map(z.string(), changesSchema),
changes: castObjectToMap(z.string(), changesSchema),
});

export type UpdatedAttachment = z.infer<typeof updatedAttachmentSchema>;
Expand Down

0 comments on commit 17d348e

Please sign in to comment.