import {z} from 'zod' export type CreateApiApiMerchantGenerateSignatureArgs = {Authorization?: string | undefined, 'X-Portone-Client-Key'?: string | undefined, 'Content-Type'?: string | undefined, body: {portone_key: string, currency: string, amount: number, merchant_order_id: string, success_url: string, failure_url: string}}; export type CreateApiApiMerchantGenerateSignatureOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const signatureHashContent = z.object({signature: z.string().optional()}); export const createApiApiMerchantGenerateSignatureResponse = z.object({content: signatureHashContent.optional(), message: z.string().optional(), status_code: z.string().optional(), status_reason: 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 }; /** * Generate signature hash * * POST /api/merchant/generate-signature */ export const createApiApiMerchantGenerateSignature = async (args: CreateApiApiMerchantGenerateSignatureArgs, options?: CreateApiApiMerchantGenerateSignatureOptions) => { const {baseUrl = "https://api.portone.cloud", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['X-Portone-Client-Key'] != null) headers.set("X-Portone-Client-Key", String(args['X-Portone-Client-Key'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/merchant/generate-signature${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(createApiApiMerchantGenerateSignatureResponse, await res.json()) };