import {z} from 'zod' export type UserSourceDynamicTenantConfig = {tenantIdFieldName: string, tenantResolverType: 'dynamic'}; export type UserSourceStaticTenantConfig = {tenantId: string, tenantResolverType: 'static'}; export type UserSourceNewTenantConfig = {tenantResolverType: 'new'}; export type ExternalAuth0UserSourceConfig = {syncOnLogin: boolean, isMigrated: boolean, domain: string, clientId: string, secret: string, tenantConfig: UserSourceDynamicTenantConfig | UserSourceStaticTenantConfig | UserSourceNewTenantConfig}; export type CreateAuth0ExternalUserSourceRequestDTO = {name: string, configuration: ExternalAuth0UserSourceConfig, appIds?: Array | undefined, index: number, description?: string | undefined}; export type CreateApiResourcesUserSourcesV1ExternalAuth0Args = {body: CreateAuth0ExternalUserSourceRequestDTO}; export type CreateApiResourcesUserSourcesV1ExternalAuth0Options = {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 Auth0 external user source * * POST /resources/user-sources/v1/external/auth0 */ export const createApiResourcesUserSourcesV1ExternalAuth0 = async (args: CreateApiResourcesUserSourcesV1ExternalAuth0Args, options?: CreateApiResourcesUserSourcesV1ExternalAuth0Options) => { 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/auth0${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()) };