import {z} from 'zod' export type CreateApiPublicV1WorkspaceGrantsArgs = {body: {workspace_id: string, user_email: string}}; export type CreateApiPublicV1WorkspaceGrantsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPublicV1WorkspaceGrantsResponse = z.object({statusCode: z.number().optional(), status: z.boolean().optional(), message: z.string().optional(), data: z.object({grant: z.object({user_id: z.string().optional(), user_email: z.string().optional(), workspace_id: z.string().optional(), granted_by_user_id: z.string().optional(), created_at: z.string().optional()}).optional()}).optional()}); 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 }; /** * Grant a user's API key visibility into a workspace without a workspace membership. The granted user gains MEMBER-level access on grant-aware Public API endpoints (workspace listing and endpoints resolving scope through the shared workspace-scope helpers); remaining endpoints are being rolled onto the grant-aware path. Requires an OWNER or ADMIN membership on the target workspace (or a workspace key for it) — a key can never grant itself access. * * POST /public/v1/workspace-grants */ export const createApiPublicV1WorkspaceGrants = async (args: CreateApiPublicV1WorkspaceGrantsArgs, options?: CreateApiPublicV1WorkspaceGrantsOptions) => { const {baseUrl = "https://api.joinvalley.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/workspace-grants${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(createApiPublicV1WorkspaceGrantsResponse, await res.json()) };