import {z} from 'zod' export type BackupProvider = 's3' | 'spaces' | 'hetzner' | 'ovh' | 'scaleway' | 'custom'; export type CreateStorageConfigurationRequest = {name: string, provider: BackupProvider, region?: string | undefined, bucket?: (string | null) | undefined, directory?: (string | null) | undefined, endpoint?: string | undefined, access_key?: (string | null) | undefined, secret_key?: (string | null) | undefined, assume_role?: boolean | undefined}; export type CreateApiOrgsOrganizationStorageProvidersArgs = {organization: string, body: CreateStorageConfigurationRequest}; export type CreateApiOrgsOrganizationStorageProvidersOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const backupProvider = z.enum(["s3", "spaces", "hetzner", "ovh", "scaleway", "custom"]); export const link = z.object({href: z.string(), rel: z.string().optional(), describedby: z.string().optional(), title: z.string().optional(), type: z.string().optional(), hreflang: z.union([z.string(), z.array(z.string())]).optional(), meta: z.object({}).optional()}); export const storageProviderResource = z.object({id: z.string(), type: z.literal("storageProviders"), attributes: z.object({name: z.string(), provider: backupProvider, provider_name: z.enum(["Amazon S3", "DigitalOcean Spaces", "Hetzner", "OVH", "Scaleway", "Custom (S3 Compatible)"]), region: z.string().nullable(), bucket: z.string().nullable(), directory: z.string().nullable(), endpoint: z.string().nullable(), assume_role: z.boolean().nullable(), in_use: z.boolean(), created_at: z.string().nullable(), updated_at: z.string().nullable()}).optional(), links: z.object({self: link})}); export const createApiOrgsOrganizationStorageProvidersResponse = z.object({data: storageProviderResource}); 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 storage provider * * POST /orgs/{organization}/storage-providers */ export const createApiOrgsOrganizationStorageProviders = async (args: CreateApiOrgsOrganizationStorageProvidersArgs, options?: CreateApiOrgsOrganizationStorageProvidersOptions) => { const {baseUrl = "https://forge.laravel.com/api", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/${encodeURIComponent(String(args.organization))}/storage-providers${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(createApiOrgsOrganizationStorageProvidersResponse, await res.json()) };