@vulog/aima-promocode
Version:
```bash npm i @vulog/aima-client @vulog/aima-promocode ```
39 lines (33 loc) • 1.25 kB
text/typescript
import { Client } from '@vulog/aima-client';
import { z } from 'zod';
import { PromoCode } from './types';
export const getPromoCodeByReference = async (client: Client, reference: string): Promise<PromoCode | undefined> => {
const result = z.string().trim().min(1).safeParse(reference);
if (!result.success) {
throw new TypeError('Invalid reference', {
cause: result.error.issues,
});
}
return client
.get<
PromoCode[]
>(`boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/promoCodes?reference=${reference}`)
.then(({ data: [r] }) => r);
};
export const getPromoCodeById = async (client: Client, id: number): Promise<PromoCode | undefined> => {
const result = z.number().positive().safeParse(id);
if (!result.success) {
throw new TypeError('Invalid id', {
cause: result.error.issues,
});
}
return client
.get<PromoCode>(`boapi/proxy/billing/fleets/${client.clientOptions.fleetId}/promoCodes/${id}`)
.then(({ data }) => data)
.catch((error: any) => {
if (error.formattedError?.status === 404) {
return undefined;
}
throw error;
});
};