import {z} from 'zod' export type CreateApiOauthApplicationsOauthApplicationIdRotateSecretArgs = {oauth_application_id: string}; export type CreateApiOauthApplicationsOauthApplicationIdRotateSecretOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const oAuthApplicationWithSecret = z.object({object: z.literal("oauth_application"), id: z.string(), instance_id: z.string(), name: z.string(), client_id: z.string(), client_uri: z.string().nullable(), client_image_url: z.string().nullable(), dynamically_registered: z.boolean(), consent_screen_enabled: z.boolean(), pkce_required: z.boolean(), audience_uri: z.string().nullable(), token_exchange_enabled: z.boolean(), device_authorization_grant_enabled: z.boolean(), public: z.boolean(), scopes: z.string(), redirect_uris: z.array(z.string()), callback_url: z.string(), authorize_url: z.string(), token_fetch_url: z.string(), user_info_url: z.string(), discovery_url: z.string(), token_introspection_url: z.string(), created_at: z.number().int(), updated_at: z.number().int(), client_secret: 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 }; /** * Rotate the client secret of the given OAuth application * * POST /oauth_applications/{oauth_application_id}/rotate_secret */ export const createApiOauthApplicationsOauthApplicationIdRotateSecret = async (args: CreateApiOauthApplicationsOauthApplicationIdRotateSecretArgs, options?: CreateApiOauthApplicationsOauthApplicationIdRotateSecretOptions) => { const {baseUrl = "https://api.clerk.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/oauth_applications/${encodeURIComponent(String(args.oauth_application_id))}/rotate_secret${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(oAuthApplicationWithSecret, await res.json()) };