import {z} from 'zod' export type IncidentMembershipsV1CreateRequestBody = {incident_id: string, user_id: string}; export type CreateApiV1IncidentMembershipsArgs = {body: IncidentMembershipsV1CreateRequestBody}; export type CreateApiV1IncidentMembershipsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userV2 = z.object({email: z.string().optional(), id: z.string(), name: z.string(), role: z.enum(["viewer", "responder", "administrator", "owner", "unset"]), slack_user_id: z.string().optional()}); export const incidentMembership = z.object({created_at: z.string(), id: z.string(), incident_id: z.string(), updated_at: z.string(), user: userV2}); export const incidentMembershipsV1CreateResponseBody = z.object({incident_membership: incidentMembership}); 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 Incident Memberships V1 * * POST /v1/incident_memberships */ export const createApiV1IncidentMemberships = async (args: CreateApiV1IncidentMembershipsArgs, options?: CreateApiV1IncidentMembershipsOptions) => { const {baseUrl = "https:////api.incident.io", 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(/\/+$/, '')}/v1/incident_memberships${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(incidentMembershipsV1CreateResponseBody, await res.json()) };