import {z} from 'zod' export type GetApiTodoTypesArgs = {'filter[trashed]'?: ('with' | 'only') | undefined, 'filter[created_from]'?: string | undefined, 'filter[created_to]'?: string | undefined, 'filter[updated_from]'?: string | undefined, 'filter[updated_to]'?: string | undefined, 'page[size]'?: number | undefined, 'page[number]'?: number | undefined}; export type GetApiTodoTypesOptions = {baseUrl?: string | undefined, yourApiToken?: string | undefined, fetch?: typeof fetch | undefined}; export const todoType = z.object({id: z.string().optional(), type: z.literal("todoType").optional(), attributes: z.object({name: z.string().optional(), colour: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), deleted_at: z.string().nullable().optional()}).optional(), relationships: z.object({}).optional()}); export const links = z.object({self: z.string().optional(), first: z.string().optional(), next: z.string().optional(), prev: z.string().optional(), last: z.string().optional()}); export const pagination = z.object({pagination: z.object({total: z.number().int().optional(), count: z.number().int().optional(), per_page: z.number().int().optional(), current_page: z.number().int().optional(), total_pages: z.number().int().optional()}).optional()}); export const meta = z.object({}); export const jsonApi = z.object({version: z.string().optional(), meta: meta.optional()}); export const getApiTodoTypesResponse = z.object({data: z.array(todoType).optional(), links: links.optional(), meta: pagination.optional(), jsonapi: jsonApi.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get all Task Types * * GET /todo-types */ export const getApiTodoTypes = async (args: GetApiTodoTypesArgs, options?: GetApiTodoTypesOptions) => { const {baseUrl = "https://street.co.uk/open-api/v1", fetch: fetchFn = fetch, yourApiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[trashed]'] != null) query.append("filter[trashed]", String(args['filter[trashed]'])) if (args['filter[created_from]'] != null) query.append("filter[created_from]", String(args['filter[created_from]'])) if (args['filter[created_to]'] != null) query.append("filter[created_to]", String(args['filter[created_to]'])) if (args['filter[updated_from]'] != null) query.append("filter[updated_from]", String(args['filter[updated_from]'])) if (args['filter[updated_to]'] != null) query.append("filter[updated_to]", String(args['filter[updated_to]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[number]'] != null) query.append("page[number]", String(args['page[number]'])) if (yourApiToken !== undefined) headers.set('authorization', `Bearer ${yourApiToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/todo-types${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiTodoTypesResponse, await res.json()) };