import {z} from 'zod' export type GetApiV1OrgsOrgIdAccessArgs = {org_id: string, include_external?: boolean | undefined, membership_requests?: boolean | undefined}; export type GetApiV1OrgsOrgIdAccessOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coreScopeConstraint = z.object({field: z.string().optional(), operator: z.string().optional(), values: z.array(z.string()).optional()}); export const coreScopeKind = z.literal("lookup_table"); export const coreAccessScope = z.object({constraints: z.array(coreScopeConstraint).optional(), kind: coreScopeKind.optional(), params: z.record(z.string(), z.unknown()).optional()}); export const coreAccess = z.object({approval_status: z.string().optional(), created: z.string().optional(), creator: z.string().optional(), external_activated: z.string().optional(), external_id: z.string().optional(), external_status: z.string().optional(), id: z.string().optional(), owner: z.string().optional(), resource: z.string().optional(), scope: coreAccessScope.optional(), type: z.string().optional(), updated: z.string().optional(), updater: z.string().optional()}); export const getApiV1OrgsOrgIdAccessResponse = z.array(coreAccess); 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 }; /** * Returns accesses for the given org. * * GET /v1/orgs/{org_id}/access */ export const getApiV1OrgsOrgIdAccess = async (args: GetApiV1OrgsOrgIdAccessArgs, options?: GetApiV1OrgsOrgIdAccessOptions) => { const {baseUrl = "https://api.edgedelta.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.include_external != null) query.append("include_external", String(args.include_external)) if (args.membership_requests != null) query.append("membership_requests", String(args.membership_requests)) if (ApiKeyAuth !== undefined) headers.set("X-ED-API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/orgs/${encodeURIComponent(String(args.org_id))}/access${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1OrgsOrgIdAccessResponse, await res.json()) };