import {z} from 'zod' export type USStates = 'AL' | 'AK' | 'AZ' | 'AR' | 'AS' | 'CA' | 'CO' | 'CT' | 'DE' | 'DC' | 'FM' | 'FL' | 'GA' | 'GU' | 'HI' | 'ID' | 'IL' | 'IN' | 'IA' | 'KS' | 'KY' | 'LA' | 'ME' | 'MD' | 'MA' | 'MH' | 'MI' | 'MN' | 'MS' | 'MO' | 'MT' | 'NE' | 'NV' | 'NH' | 'NJ' | 'NM' | 'NY' | 'NC' | 'ND' | 'MP' | 'OH' | 'OK' | 'OR' | 'PW' | 'PA' | 'PR' | 'RI' | 'SC' | 'SD' | 'TN' | 'TX' | 'UM' | 'UT' | 'VT' | 'VA' | 'VI' | 'WA' | 'WV' | 'WI' | 'WY' | 'AA' | 'AE' | 'AP'; export type BusinessLicenseState = USStates | 'CAN' | 'NLA'; export type CRBMUniformLicenseTypeDescription = 'Caregiver' | 'CBD' | 'Corporate Officer' | 'Cultivation' | 'Delivery/Transporter' | 'Event Organizer' | 'Hemp' | 'Illicit Establishment' | 'Management Company' | 'Manufacturer/Processor' | 'Owner/Investor' | 'Research' | 'Retail/Dispensary' | 'Senior Manager' | 'Social Use Club' | 'Storage' | 'Testing' | 'Unknown' | 'Vertically Integrated' | 'Waste Disposal' | 'Wholesale/Distribution'; export type GetApiServiceProvidersSpIdLicensesSearchArgs = {sp_id: string, limit?: number | undefined, offset?: number | undefined, licenseNumber?: string | undefined, name?: string | undefined, state?: BusinessLicenseState | undefined, licenseType?: CRBMUniformLicenseTypeDescription | undefined}; export type GetApiServiceProvidersSpIdLicensesSearchOptions = {baseUrl?: string | undefined, accessAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const positiveInteger = z.number().int().gte(0); export const cRBMMedRecClassification = z.enum(["both", "hemp", "medical", "recreational"]); export const cRBMLicensingAuthorityTier = z.enum(["country", "locality", "other", "region", "sub_region"]); export const cRBMUniformLicenseTypeDescription = z.enum(["Caregiver", "CBD", "Corporate Officer", "Cultivation", "Delivery/Transporter", "Event Organizer", "Hemp", "Illicit Establishment", "Management Company", "Manufacturer/Processor", "Owner/Investor", "Research", "Retail/Dispensary", "Senior Manager", "Social Use Club", "Storage", "Testing", "Unknown", "Vertically Integrated", "Waste Disposal", "Wholesale/Distribution"]); export const cRBMUniformLicenseStatusDescription = z.enum(["Active", "Approved/Pending", "Inactive", "Pre-License", "Unknown", "Unlicensed"]); export const businessLicense = z.object({licensee: z.string().nullable(), license_identifier: z.string().nullable(), med_rec_classification: cRBMMedRecClassification.nullable(), locality: z.string().nullable(), licensing_authority_name: z.string(), licensing_authority_tier: cRBMLicensingAuthorityTier, licensing_authority_address: z.object({postal_code: z.string().nullable(), sub_region: z.string().nullable(), region: z.string().nullable(), created_at: z.string().nullable(), locality: z.string().nullable(), street: z.string().nullable(), premise: z.string().nullable(), updated_at: z.string().nullable(), sub_locality: z.string().nullable(), box_number: z.string().nullable(), country: z.string().nullable(), sub_premise: z.string().nullable()}), state_license_class_description: z.string().nullable(), state_license_status_description: z.string().nullable(), state_license_sub_class_description: z.string().nullable(), reported_license_type_description: z.string(), reported_license_status: z.string(), reported_license_sub_type: z.string().nullable(), uniform_license_type_description: cRBMUniformLicenseTypeDescription, uniform_license_status_description: cRBMUniformLicenseStatusDescription, status_date: z.string(), issued_at: z.string().nullable(), expires_at: z.string().nullable(), created_at: z.string(), updated_at: z.string(), edited_at: z.string(), businesses: z.array(z.object({name: z.string()})), individuals: z.array(z.object({name: z.string()}))}); export const pagedBusinessLicenseResponse = z.object({metadata: z.object({total: positiveInteger, count: z.string()}), data: z.array(businessLicense)}); 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 }; /** * Search for cannabis licenses * * GET /service-providers/{sp_id}/licenses-search */ export const getApiServiceProvidersSpIdLicensesSearch = async (args: GetApiServiceProvidersSpIdLicensesSearchArgs, options?: GetApiServiceProvidersSpIdLicensesSearchOptions) => { const {baseUrl = "https://sandbox-api.greencheckverified.com", fetch: fetchFn = fetch, accessAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.licenseNumber != null) query.append("licenseNumber", String(args.licenseNumber)) if (args.name != null) query.append("name", String(args.name)) if (args.state != null) query.append("state", String(args.state)) if (args.licenseType != null) query.append("licenseType", String(args.licenseType)) if (accessAuth !== undefined) headers.set("Authorization", accessAuth) const url = `${baseUrl.replace(/\/+$/, '')}/service-providers/${encodeURIComponent(String(args.sp_id))}/licenses-search${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(pagedBusinessLicenseResponse, await res.json()) };