import {z} from 'zod' export type OrganizationMemberRoleEDITOR = 'editor'; export type OrganizationMemberRoleINTERACTIVEVIEWER = 'interactive_viewer'; export type OrganizationMemberRoleVIEWER = 'viewer'; export type OrganizationMemberRoleMEMBER = 'member'; export type AllowedEmailDomainsRole = OrganizationMemberRoleEDITOR | OrganizationMemberRoleINTERACTIVEVIEWER | OrganizationMemberRoleVIEWER | OrganizationMemberRoleMEMBER; export type ProjectMemberRoleEDITOR = 'editor'; export type ProjectMemberRoleINTERACTIVEVIEWER = 'interactive_viewer'; export type ProjectMemberRoleVIEWER = 'viewer'; export type AllowedEmailDomainProjectsRole = ProjectMemberRoleEDITOR | ProjectMemberRoleINTERACTIVEVIEWER | ProjectMemberRoleVIEWER; export type PickAllowedEmailDomainsExcludeKeyofAllowedEmailDomainsOrganizationUuid = {role: AllowedEmailDomainsRole, emailDomains: Array, projects: Array<{role: AllowedEmailDomainProjectsRole, projectUuid: string}>}; export type UpdateAllowedEmailDomains = PickAllowedEmailDomainsExcludeKeyofAllowedEmailDomainsOrganizationUuid; export type PatchApiApiV1OrgAllowedEmailDomainsArgs = {body: UpdateAllowedEmailDomains}; export type PatchApiApiV1OrgAllowedEmailDomainsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const projectMemberRoleEDITOR = z.literal("editor"); export const projectMemberRoleINTERACTIVEVIEWER = z.literal("interactive_viewer"); export const projectMemberRoleVIEWER = z.literal("viewer"); export const allowedEmailDomainProjectsRole = z.union([projectMemberRoleEDITOR, projectMemberRoleINTERACTIVEVIEWER, projectMemberRoleVIEWER]); export const organizationMemberRoleEDITOR = z.literal("editor"); export const organizationMemberRoleINTERACTIVEVIEWER = z.literal("interactive_viewer"); export const organizationMemberRoleVIEWER = z.literal("viewer"); export const organizationMemberRoleMEMBER = z.literal("member"); export const allowedEmailDomainsRole = z.union([organizationMemberRoleEDITOR, organizationMemberRoleINTERACTIVEVIEWER, organizationMemberRoleVIEWER, organizationMemberRoleMEMBER]); export const allowedEmailDomains = z.object({projects: z.array(z.object({role: allowedEmailDomainProjectsRole, projectUuid: z.string()})), role: allowedEmailDomainsRole, emailDomains: z.array(z.string()), organizationUuid: z.string()}); export const apiOrganizationAllowedEmailDomains = z.object({results: allowedEmailDomains, status: z.literal("ok")}); 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 }; /** * Update allowed email domains * * PATCH /api/v1/org/allowedEmailDomains */ export const patchApiApiV1OrgAllowedEmailDomains = async (args: PatchApiApiV1OrgAllowedEmailDomainsArgs, options: PatchApiApiV1OrgAllowedEmailDomainsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/org/allowedEmailDomains${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(apiOrganizationAllowedEmailDomains, await res.json()) };