import {z} from 'zod' export type BusinessEntityID = string; export type EmissionsSourceTypeID = string; export type EmissionsModelID = string; export type EmissionsSourceID = string; export type EmissionsScope = number; export type EmissionsSourceStatus = 'ACTIVE' | 'INACTIVE'; export type CreateApiV1OrganizationEmissionsSourcesArgs = {body: {processID: BusinessEntityID, emissionsSourceTypeID: EmissionsSourceTypeID, emissionsModelID: EmissionsModelID, relatedSourceID?: EmissionsSourceID | undefined, scope?: EmissionsScope | undefined, status?: EmissionsSourceStatus | undefined, description?: string | undefined, referenceSourceIDs?: Array | undefined}}; export type CreateApiV1OrganizationEmissionsSourcesOptions = {baseUrl?: string | undefined, OAuth2ClientCredentialsProduction?: string | undefined, fetch?: typeof fetch | undefined}; export const emissionsSourceID = z.string(); export const businessEntityID = z.string(); export const emissionsSourceTypeID = z.string(); export const emissionsModelID = z.string(); export const nullableEmissionsSourceID = z.string().nullable(); export const emissionsScope = z.union([z.literal(1), z.literal(2), z.literal(21), z.literal(22), z.literal(3), z.literal(-1)]); export const emissionsSourceStatus = z.enum(["ACTIVE", "INACTIVE"]); export const emissionsSource = z.object({id: emissionsSourceID, processID: businessEntityID, emissionsSourceTypeID: emissionsSourceTypeID, emissionsModelID: emissionsModelID, relatedSourceID: nullableEmissionsSourceID.optional(), scope: emissionsScope, status: emissionsSourceStatus, description: z.string().optional(), referenceSourceIDs: z.array(emissionsSourceID).nullable().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 }; /** * Create a new emissions source * * POST /v1/organization/emissions_sources * * Also accepts auth: OAuth2AuthCodeProduction */ export const createApiV1OrganizationEmissionsSources = async (args: CreateApiV1OrganizationEmissionsSourcesArgs, options?: CreateApiV1OrganizationEmissionsSourcesOptions) => { const {baseUrl = "https://api.sinai.com", fetch: fetchFn = fetch, OAuth2ClientCredentialsProduction} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentialsProduction !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentialsProduction}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organization/emissions_sources${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(emissionsSource, await res.json()) };