import {z} from 'zod' export type GetApiPackagesPackageIdGranulesArgs = {packageId: string, offsetMark?: string | undefined, offset?: number | undefined, pageSize: number, md5?: string | undefined, granuleClass?: string | undefined}; export type GetApiPackagesPackageIdGranulesOptions = {baseUrl: string, apiKeyScheme?: string | undefined, fetch?: typeof fetch | undefined}; export const granuleMetadata = z.object({title: z.string().optional(), granuleId: z.string().optional(), granuleLink: z.string().optional(), dateIssued: z.string().optional(), granuleClass: z.string().optional(), md5: z.string().optional()}); export const granuleContainer = z.object({count: z.number().int().optional(), offset: z.number().int().optional(), pageSize: z.number().int().optional(), nextPage: z.string().optional(), previousPage: z.string().optional(), granules: z.array(granuleMetadata).optional(), message: z.string().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 }; /** * Get a list of granules associated with a package * * GET /packages/{packageId}/granules */ export const getApiPackagesPackageIdGranules = async (args: GetApiPackagesPackageIdGranulesArgs, options: GetApiPackagesPackageIdGranulesOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyScheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offsetMark != null) query.append("offsetMark", String(args.offsetMark)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.md5 != null) query.append("md5", String(args.md5)) if (args.granuleClass != null) query.append("granuleClass", String(args.granuleClass)) if (apiKeyScheme !== undefined) query.set("api_key", apiKeyScheme) const url = `${baseUrl.replace(/\/+$/, '')}/packages/${encodeURIComponent(String(args.packageId))}/granules${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(granuleContainer, await res.json()) };