import {z} from 'zod' export type FirebaseServiceAccountConfig = {type: string, project_id: string, private_key_id: string, private_key: string, client_email: string, client_id: string, auth_uri: string, token_uri: string, auth_provider_x509_cert_url: string, client_x509_cert_url: string, universe_domain: string}; export type UserSourceStaticTenantConfig = {tenantId: string, tenantResolverType: 'static'}; export type UserSourceNewTenantConfig = {tenantResolverType: 'new'}; export type ExternalFirebaseUserSourceConfig = {syncOnLogin: boolean, isMigrated: boolean, apiKey: string, serviceAccount: FirebaseServiceAccountConfig, tenantConfig: UserSourceStaticTenantConfig | UserSourceNewTenantConfig}; export type CreateFirebaseExternalUserSourceRequestDTO = {name: string, configuration: ExternalFirebaseUserSourceConfig, appIds?: Array | undefined, index: number, description?: string | undefined}; export type CreateApiResourcesUserSourcesV1ExternalFirebaseArgs = {body: CreateFirebaseExternalUserSourceRequestDTO}; export type CreateApiResourcesUserSourcesV1ExternalFirebaseOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createUserSourceResponse = z.object({id: z.string(), name: z.string(), type: z.string(), appIds: z.array(z.string()), description: z.string(), index: z.number()}); 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 Firebase external user source * * POST /resources/user-sources/v1/external/firebase */ export const createApiResourcesUserSourcesV1ExternalFirebase = async (args: CreateApiResourcesUserSourcesV1ExternalFirebaseArgs, options?: CreateApiResourcesUserSourcesV1ExternalFirebaseOptions) => { const {baseUrl = "http://localhost:8181", 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 ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/user-sources/v1/external/firebase${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(createUserSourceResponse, await res.json()) };