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 ExternalCognitoUserSourceConfig = {syncOnLogin: boolean, isMigrated: boolean, region: string, clientId: string, userPoolId: string, accessKeyId: string, secretAccessKey: string, clientSecret?: string | undefined, tenantConfig: UserSourceDynamicTenantConfig | UserSourceStaticTenantConfig | UserSourceNewTenantConfig}; export type CreateCognitoExternalUserSourceRequestDTO = {name: string, configuration: ExternalCognitoUserSourceConfig, appIds?: Array | undefined, index: number, description?: string | undefined}; export type CreateApiResourcesUserSourcesV1ExternalCognitoArgs = {body: CreateCognitoExternalUserSourceRequestDTO}; export type CreateApiResourcesUserSourcesV1ExternalCognitoOptions = {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 Cognito external user source * * POST /resources/user-sources/v1/external/cognito */ export const createApiResourcesUserSourcesV1ExternalCognito = async (args: CreateApiResourcesUserSourcesV1ExternalCognitoArgs, options?: CreateApiResourcesUserSourcesV1ExternalCognitoOptions) => { 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/cognito${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()) };