Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
".": "./dist/src/index.js",
"./auth": "./dist/src/auth/index.js",
"./deliveries": "./dist/src/deliveries/index.js",
"./organizations": "./dist/src/organizations/index.js"
"./organizations": "./dist/src/organizations/index.js",
"./types": "./dist/src/types/index.js"
},
"files": [
"dist"
],
"scripts": {
"prepare": "npm run build",
"build": "tsc",
"build": "tsc && npm run copy:types",
"copy:types": "node ./scripts/copyTypes.js",
"lint": "eslint . --ext .ts",
"types": "./scripts/generateTypes.sh",
"test": "jest --coverage",
Expand Down
28 changes: 28 additions & 0 deletions scripts/copyTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require('fs/promises');
const path = require('path');

async function copyTypes() {
const sourceDir = path.join(__dirname, '..', 'src', 'types');
const destDir = path.join(__dirname, '..', 'dist', 'src', 'types');

const entries = await fs.readdir(sourceDir, { withFileTypes: true });
const typeFiles = entries.filter(
(entry) => entry.isFile() && entry.name.endsWith('.d.ts')
);

await fs.mkdir(destDir, { recursive: true });

await Promise.all(
typeFiles.map((entry) =>
fs.copyFile(
path.join(sourceDir, entry.name),
path.join(destDir, entry.name)
)
)
);
}

copyTypes().catch((error) => {
console.error('copyTypes failed:', error);
process.exit(1);
});
5 changes: 4 additions & 1 deletion src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export async function getAccessToken(): Promise<string> {

if (response.ok) {
const data: LoginResp = await response.json();
return data.access_token as string;
if (!data.access_token) {
throw new Error('Access token is missing in response');
}
return data.access_token;
} else {
const error = await response.text();
throw new Error(`Failed to fetch access token: ${error}`);
Expand Down
19 changes: 12 additions & 7 deletions src/deliveries/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type {
CreateDeliveryResp,
DeliveryQuoteReq,
DeliveryQuoteResp,
DeliveryReq,
DeliveryResp,
ListDeliveriesReq,
ListDeliveriesResp,
PODReq,
PODResp,
UpdateDeliveryReq,
} from './types';
import { fetchData, makeQueryString } from '../utils';
Expand All @@ -27,7 +32,7 @@ export class DeliveriesClient {
}

async createQuote(req: DeliveryQuoteReq) {
return fetchData(
return fetchData<DeliveryQuoteResp>(
`${this.baseURL}/delivery_quotes`,
'POST',
this.accessToken,
Expand All @@ -36,7 +41,7 @@ export class DeliveriesClient {
}

async createDelivery(req: DeliveryReq) {
return fetchData(
return fetchData<CreateDeliveryResp>(
`${this.baseURL}/deliveries`,
'POST',
this.accessToken,
Expand All @@ -45,7 +50,7 @@ export class DeliveriesClient {
}

async getDelivery(deliveryId: string) {
return fetchData(
return fetchData<DeliveryResp>(
`${this.baseURL}/deliveries/${deliveryId}`,
'GET',
this.accessToken
Expand All @@ -57,19 +62,19 @@ export class DeliveriesClient {
if (options) {
url += makeQueryString(options);
}
return fetchData(url, 'GET', this.accessToken);
return fetchData<ListDeliveriesResp>(url, 'GET', this.accessToken);
}

async cancelDelivery(deliveryId: string) {
return fetchData(
return fetchData<DeliveryResp>(
`${this.baseURL}/deliveries/${deliveryId}/cancel`,
'POST',
this.accessToken
);
}

async updateDelivery(deliveryId: string, req: UpdateDeliveryReq) {
return fetchData(
return fetchData<DeliveryResp>(
`${this.baseURL}/deliveries/${deliveryId}`,
'POST',
this.accessToken,
Expand All @@ -78,7 +83,7 @@ export class DeliveriesClient {
}

async proofOfDelivery(deliveryId: string, req: PODReq) {
return fetchData(
return fetchData<PODResp>(
`${this.baseURL}/deliveries/${deliveryId}/proof-of-delivery`,
'POST',
this.accessToken,
Expand Down
2 changes: 2 additions & 0 deletions src/deliveries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type DeliveryQuoteReq = components['schemas']['DeliveryQuoteReq'];
type DeliveryQuoteResp = components['schemas']['DeliveryQuoteResp'];
type DeliveryReq = components['schemas']['DeliveryReq'];
type DeliveryResp = components['schemas']['DeliveryResp'];
type CreateDeliveryResp = components['schemas']['CreateDeliveryResp'];
type UpdateDeliveryReq = components['schemas']['UpdateDeliveryReq'];
type ListDeliveriesReq = paths['/customers/{customer_id}/deliveries']['get']['parameters']['query'];
type ListDeliveriesResp =
Expand All @@ -28,6 +29,7 @@ export type {
DeliveryQuoteResp,
DeliveryReq,
DeliveryResp,
CreateDeliveryResp,
Error,
ListDeliveriesReq,
ListDeliveriesResp,
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { getAccessToken } from './auth';
export { DeliveriesClient, createDeliveriesClient } from './deliveries';
export { OrganizationsClient, createOrganizationsClient } from './organizations';
export { AuthTypes, DeliveriesTypes, OrganizationsTypes } from './types';
export { FetchError } from './utils';
9 changes: 4 additions & 5 deletions src/organizations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CreateOrgReq } from './types';
import type { InviteMemberReq } from './types';
import type { CreateOrgResp, CreateOrgReq,InviteMemberResp, InviteMemberReq } from './types';
import { fetchData } from '../utils';

export class OrganizationsClient {
Expand All @@ -12,15 +11,15 @@ export class OrganizationsClient {
}

async createOrganization(req: CreateOrgReq) {
return fetchData(`${this.baseURL}/organizations`, 'POST', this.accessToken, req);
return fetchData<CreateOrgResp>(`${this.baseURL}/organizations`, 'POST', this.accessToken, req);
}

async inviteMember(organizationId: string, req: InviteMemberReq) {
return fetchData(`${this.baseURL}/organizations/${organizationId}/memberships/invite`, 'POST', this.accessToken, req);
return fetchData<InviteMemberResp>(`${this.baseURL}/organizations/${organizationId}/memberships/invite`, 'POST', this.accessToken, req);
}

async getOrganization(organizationId: string) {
return fetchData(`${this.baseURL}/organizations/${organizationId}`, 'GET', this.accessToken);
return fetchData<CreateOrgResp>(`${this.baseURL}/organizations/${organizationId}`, 'GET', this.accessToken);
}


Expand Down
5 changes: 5 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as AuthTypes from "./auth";
import * as DeliveriesTypes from "./deliveries";
import * as OrganizationsTypes from "./organizations";

export { AuthTypes, DeliveriesTypes, OrganizationsTypes };
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ const makeQueryString = (params: Record<string, unknown>) => {
return `?${query}`;
};

const fetchData = async <T = any>(
const fetchData = async <T>(
url: string,
method: 'GET' | 'POST',
accessToken: string,
req?: Record<string, unknown>
) => {
): Promise<T> => {
const headers: Record<string, string> = getHeaders(accessToken, method);

const options: RequestInit = {
Expand All @@ -76,7 +76,7 @@ const fetchData = async <T = any>(
const response = await fetch(url, options);
if (response.ok) {
const data = await response.json();
return data;
return data as T;
} else {
const errorResponse: ApiError = await response.json();
throw new FetchError(
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"src/index.ts",
"src/auth/index.ts",
"src/deliveries/index.ts",
"src/organizations/index.ts"
"src/organizations/index.ts",
"src/types/index.ts"
],
"exclude": ["node_modules"]
}