import {z} from 'zod' export type GetApiCompaniesCompanyIdSkillsArgs = {companyId: number}; export type GetApiCompaniesCompanyIdSkillsOptions = {baseUrl: string, jobadderIdentity?: string | undefined, fetch?: typeof fetch | undefined}; export const skillCategoryModel = z.object({skillId: z.number().int(), name: z.string().min(1)}); export const subCategoryModel = z.object({subCategoryId: z.number().int(), name: z.string().min(1), skills: z.array(skillCategoryModel).nullable().optional()}); export const categoryModel = z.object({categoryId: z.number().int(), name: z.string().min(1), subCategories: z.array(subCategoryModel).nullable().optional()}); export const modelLinks = z.object({self: z.string().nullable().optional()}); export const categoryListRepresentation = z.object({items: z.array(categoryModel).nullable().optional(), links: modelLinks.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 company skills * * GET /companies/{companyId}/skills */ export const getApiCompaniesCompanyIdSkills = async (args: GetApiCompaniesCompanyIdSkillsArgs, options: GetApiCompaniesCompanyIdSkillsOptions) => { const {baseUrl, fetch: fetchFn = fetch, jobadderIdentity} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (jobadderIdentity !== undefined) headers.set('authorization', `Bearer ${jobadderIdentity}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.companyId))}/skills${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(categoryListRepresentation, await res.json()) };