import {z} from 'zod' export type OauthApplicationInCreate = {name: string, redirectUri: string, makePublic?: boolean | undefined, description?: string | undefined}; export type BodyOauthNewApplicationHandlerApiV5OauthApplicationsNewPost = {oauth_application: OauthApplicationInCreate}; export type CreateApiApiV5OauthApplicationsNewArgs = {body: BodyOauthNewApplicationHandlerApiV5OauthApplicationsNewPost}; export type CreateApiApiV5OauthApplicationsNewOptions = {baseUrl?: string | undefined, OAuth2PasswordBearerCookie?: string | undefined, fetch?: typeof fetch | undefined}; export const oauthApplication = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), name: z.string(), uid: z.string(), secret: z.string(), redirectUri: z.string(), scopes: z.string().optional(), ownerId: z.number().int().optional(), ownerType: z.string().optional(), appUrl: z.string().optional(), appIcon: z.string().optional(), approved: z.boolean().optional(), public: z.boolean().optional(), voltCost: z.number().int().optional(), makePublic: z.boolean().optional(), publisherName: z.string().optional(), description: z.string().optional(), contactEmail: z.string().optional(), habitCategoryId: z.number().int().optional()}); export const oauthApplicationInResponse = z.object({oauthApplication: oauthApplication}); 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 }; /** * Create new Oauth app * * POST /api/v5/oauth/applications/new */ export const createApiApiV5OauthApplicationsNew = async (args: CreateApiApiV5OauthApplicationsNewArgs, options?: CreateApiApiV5OauthApplicationsNewOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, OAuth2PasswordBearerCookie} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearerCookie !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearerCookie}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/oauth/applications/new${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(oauthApplicationInResponse, await res.json()) };