import {z} from 'zod' export type StringFilter = {eq?: string | undefined, ne?: string | undefined, in?: string | undefined, nin?: string | undefined, between?: string | undefined, contains?: string | undefined, starts?: string | undefined, ends?: string | undefined}; export type DateTimeFilter = {eq?: string | undefined, ne?: string | undefined, between?: string | undefined, gt?: string | undefined, gte?: string | undefined, lt?: string | undefined, lte?: string | undefined}; export type GetApiOrganizationsOrganizationIdProjectsArgs = {organization_id: unknown, 'filter[id]'?: StringFilter | undefined, 'filter[title]'?: StringFilter | undefined, 'filter[status]'?: StringFilter | undefined, 'filter[updated_at]'?: DateTimeFilter | undefined, 'filter[created_at]'?: DateTimeFilter | undefined, 'page[size]'?: number | undefined, 'page[before]'?: string | undefined, 'page[after]'?: string | undefined, sort?: string | undefined}; export type GetApiOrganizationsOrganizationIdProjectsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const projectID = z.string(); export const organizationID = z.string(); export const subscriptionID = z.string(); export const regionID = z.string(); export const projectTitle = z.string(); export const projectType = z.enum(["grid", "dedicated"]); export const projectPlan = z.string(); export const projectTimeZone = z.string(); export const projectDefaultBranch = z.string(); export const projectStatus = z.enum(["requested", "active", "failed", "suspended", "deleted"]); export const projectTrialPlan = z.boolean(); export const projectUI = z.string(); export const projectLocked = z.boolean(); export const projectCSENotes = z.string(); export const projectDedicatedTag = z.string(); export const createdAt = z.string(); export const updatedAt = z.string(); export const organizationProject = z.object({id: projectID.optional(), organization_id: organizationID.optional(), subscription_id: subscriptionID.optional(), vendor: z.string().optional(), region: regionID.optional(), title: projectTitle.optional(), type: projectType.optional(), plan: projectPlan.optional(), timezone: projectTimeZone.optional(), default_branch: projectDefaultBranch.optional(), status: projectStatus.optional(), trial_plan: projectTrialPlan.optional(), project_ui: projectUI.optional(), locked: projectLocked.optional(), cse_notes: projectCSENotes.optional(), dedicated_tag: projectDedicatedTag.optional(), created_at: createdAt.optional(), updated_at: updatedAt.optional(), _links: z.object({self: z.object({href: z.string().optional()}).optional(), update: z.object({href: z.string().optional(), method: z.string().optional()}).optional(), delete: z.object({href: z.string().optional(), method: z.string().optional()}).optional(), activities: z.object({href: z.string().optional()}).optional(), addons: z.object({href: z.string().optional()}).optional()}).optional()}); export const link = z.object({href: z.string().optional()}); export const listLinks = z.object({self: link.optional(), previous: link.optional(), next: link.optional()}); export const getApiOrganizationsOrganizationIdProjectsResponse = z.object({items: z.array(organizationProject).optional(), _links: listLinks.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 }; /** * List projects * * GET /organizations/{organization_id}/projects */ export const getApiOrganizationsOrganizationIdProjects = async (args: GetApiOrganizationsOrganizationIdProjectsArgs, options?: GetApiOrganizationsOrganizationIdProjectsOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[id]'] != null) query.append("filter[id]", JSON.stringify(args['filter[id]'])) if (args['filter[title]'] != null) query.append("filter[title]", JSON.stringify(args['filter[title]'])) if (args['filter[status]'] != null) query.append("filter[status]", JSON.stringify(args['filter[status]'])) if (args['filter[updated_at]'] != null) query.append("filter[updated_at]", JSON.stringify(args['filter[updated_at]'])) if (args['filter[created_at]'] != null) query.append("filter[created_at]", JSON.stringify(args['filter[created_at]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[before]'] != null) query.append("page[before]", String(args['page[before]'])) if (args['page[after]'] != null) query.append("page[after]", String(args['page[after]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.organization_id))}/projects${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(getApiOrganizationsOrganizationIdProjectsResponse, await res.json()) };