import {z} from 'zod' export type CreateApiAccountJwtCreateArgs = {body: {username: string, password: string}}; export type CreateApiAccountJwtCreateOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAccountJwtCreateResponse = z.object({refresh: z.string().optional(), access: z.string().optional(), pk: z.number().int().optional(), uuid: z.string().optional(), expire_duration: z.number().int().optional(), expires_at: z.string().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 }; /** * Takes a set of user credentials and returns an access and refresh JSON web token pair to prove the authentication of those credentials. * * POST /account/jwt/create/ */ export const createApiAccountJwtCreate = async (args: CreateApiAccountJwtCreateArgs, options?: CreateApiAccountJwtCreateOptions) => { const {baseUrl = "https://api.ganje.net/v1.1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/account/jwt/create/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAccountJwtCreateResponse, await res.json()) };