@mohasinac/shipping-shiprocket 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +176 -0
- package/dist/index.d.cts +236 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.js +144 -0
- package/package.json +31 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __objRest = (source, exclude) => {
|
|
24
|
+
var target = {};
|
|
25
|
+
for (var prop in source)
|
|
26
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
+
target[prop] = source[prop];
|
|
28
|
+
if (source != null && __getOwnPropSymbols)
|
|
29
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
+
target[prop] = source[prop];
|
|
32
|
+
}
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
var __export = (target, all) => {
|
|
36
|
+
for (var name in all)
|
|
37
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
38
|
+
};
|
|
39
|
+
var __copyProps = (to, from, except, desc) => {
|
|
40
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
41
|
+
for (let key of __getOwnPropNames(from))
|
|
42
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
43
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
44
|
+
}
|
|
45
|
+
return to;
|
|
46
|
+
};
|
|
47
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
48
|
+
|
|
49
|
+
// src/index.ts
|
|
50
|
+
var index_exports = {};
|
|
51
|
+
__export(index_exports, {
|
|
52
|
+
SHIPROCKET_TOKEN_TTL_MS: () => SHIPROCKET_TOKEN_TTL_MS,
|
|
53
|
+
isShiprocketTokenExpired: () => isShiprocketTokenExpired,
|
|
54
|
+
shiprocketAddPickupLocation: () => shiprocketAddPickupLocation,
|
|
55
|
+
shiprocketAuthenticate: () => shiprocketAuthenticate,
|
|
56
|
+
shiprocketCheckServiceability: () => shiprocketCheckServiceability,
|
|
57
|
+
shiprocketCreateOrder: () => shiprocketCreateOrder,
|
|
58
|
+
shiprocketGenerateAWB: () => shiprocketGenerateAWB,
|
|
59
|
+
shiprocketGeneratePickup: () => shiprocketGeneratePickup,
|
|
60
|
+
shiprocketGetPickupLocations: () => shiprocketGetPickupLocations,
|
|
61
|
+
shiprocketTrackByAWB: () => shiprocketTrackByAWB,
|
|
62
|
+
shiprocketVerifyPickupOTP: () => shiprocketVerifyPickupOTP
|
|
63
|
+
});
|
|
64
|
+
module.exports = __toCommonJS(index_exports);
|
|
65
|
+
var BASE_URL = "https://apiv2.shiprocket.in/v1/external";
|
|
66
|
+
async function shiprocketFetch(endpoint, options = {}) {
|
|
67
|
+
var _b;
|
|
68
|
+
const _a = options, { token, headers = {} } = _a, rest = __objRest(_a, ["token", "headers"]);
|
|
69
|
+
const reqHeaders = __spreadValues({
|
|
70
|
+
"Content-Type": "application/json"
|
|
71
|
+
}, headers);
|
|
72
|
+
if (token) reqHeaders["Authorization"] = `Bearer ${token}`;
|
|
73
|
+
const response = await fetch(`${BASE_URL}${endpoint}`, __spreadProps(__spreadValues({}, rest), {
|
|
74
|
+
headers: reqHeaders
|
|
75
|
+
}));
|
|
76
|
+
const text = await response.text();
|
|
77
|
+
let json;
|
|
78
|
+
try {
|
|
79
|
+
json = JSON.parse(text);
|
|
80
|
+
} catch (e) {
|
|
81
|
+
throw new Error(
|
|
82
|
+
`Shiprocket API non-JSON response ${response.status}: ${text.slice(0, 200)}`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (!response.ok) {
|
|
86
|
+
const message = (_b = json == null ? void 0 : json.message) != null ? _b : `Shiprocket API error ${response.status}`;
|
|
87
|
+
throw new Error(message);
|
|
88
|
+
}
|
|
89
|
+
return json;
|
|
90
|
+
}
|
|
91
|
+
async function shiprocketAuthenticate(credentials) {
|
|
92
|
+
return shiprocketFetch("/auth/login", {
|
|
93
|
+
method: "POST",
|
|
94
|
+
body: JSON.stringify(credentials)
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async function shiprocketGetPickupLocations(token) {
|
|
98
|
+
return shiprocketFetch(
|
|
99
|
+
"/settings/company/pickup",
|
|
100
|
+
{ token }
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
async function shiprocketAddPickupLocation(token, data) {
|
|
104
|
+
return shiprocketFetch(
|
|
105
|
+
"/settings/company/addpickup",
|
|
106
|
+
{ method: "POST", token, body: JSON.stringify(data) }
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
async function shiprocketVerifyPickupOTP(token, data) {
|
|
110
|
+
return shiprocketFetch(
|
|
111
|
+
"/settings/company/verifyOtpForPickup",
|
|
112
|
+
{ method: "POST", token, body: JSON.stringify(data) }
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
async function shiprocketCreateOrder(token, order) {
|
|
116
|
+
return shiprocketFetch(
|
|
117
|
+
"/orders/create/adhoc",
|
|
118
|
+
{ method: "POST", token, body: JSON.stringify(order) }
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
async function shiprocketGenerateAWB(token, params) {
|
|
122
|
+
return shiprocketFetch("/courier/assign/awb", {
|
|
123
|
+
method: "POST",
|
|
124
|
+
token,
|
|
125
|
+
body: JSON.stringify(params)
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async function shiprocketGeneratePickup(token, params) {
|
|
129
|
+
return shiprocketFetch("/courier/generate/pickup", {
|
|
130
|
+
method: "POST",
|
|
131
|
+
token,
|
|
132
|
+
body: JSON.stringify(params)
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async function shiprocketTrackByAWB(token, awb) {
|
|
136
|
+
return shiprocketFetch(
|
|
137
|
+
`/courier/track/awb/${awb}`,
|
|
138
|
+
{ token }
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
async function shiprocketCheckServiceability(token, paramsOrPickup, deliveryPostcode, weight, cod = false) {
|
|
142
|
+
const params = typeof paramsOrPickup === "string" ? {
|
|
143
|
+
pickup_postcode: paramsOrPickup,
|
|
144
|
+
delivery_postcode: deliveryPostcode != null ? deliveryPostcode : "",
|
|
145
|
+
weight: weight != null ? weight : 0,
|
|
146
|
+
cod: cod ? 1 : 0
|
|
147
|
+
} : paramsOrPickup;
|
|
148
|
+
const sp = new URLSearchParams();
|
|
149
|
+
sp.set("pickup_postcode", params.pickup_postcode);
|
|
150
|
+
sp.set("delivery_postcode", params.delivery_postcode);
|
|
151
|
+
sp.set("weight", String(params.weight));
|
|
152
|
+
if (params.cod !== void 0) sp.set("cod", String(params.cod));
|
|
153
|
+
return shiprocketFetch(
|
|
154
|
+
`/courier/serviceability/?${sp.toString()}`,
|
|
155
|
+
{ token }
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
var SHIPROCKET_TOKEN_TTL_MS = 9 * 24 * 60 * 60 * 1e3;
|
|
159
|
+
function isShiprocketTokenExpired(expiry) {
|
|
160
|
+
if (!expiry) return true;
|
|
161
|
+
return /* @__PURE__ */ new Date() >= new Date(expiry);
|
|
162
|
+
}
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
SHIPROCKET_TOKEN_TTL_MS,
|
|
166
|
+
isShiprocketTokenExpired,
|
|
167
|
+
shiprocketAddPickupLocation,
|
|
168
|
+
shiprocketAuthenticate,
|
|
169
|
+
shiprocketCheckServiceability,
|
|
170
|
+
shiprocketCreateOrder,
|
|
171
|
+
shiprocketGenerateAWB,
|
|
172
|
+
shiprocketGeneratePickup,
|
|
173
|
+
shiprocketGetPickupLocations,
|
|
174
|
+
shiprocketTrackByAWB,
|
|
175
|
+
shiprocketVerifyPickupOTP
|
|
176
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @mohasinac/shipping-shiprocket — Shiprocket shipping API client
|
|
3
|
+
*
|
|
4
|
+
* Thin HTTP client for the Shiprocket REST API.
|
|
5
|
+
* No credential management — caller is responsible for obtaining and
|
|
6
|
+
* passing the JWT token (see your project's platform auth layer).
|
|
7
|
+
*
|
|
8
|
+
* API base: https://apiv2.shiprocket.in/v1/external
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {
|
|
13
|
+
* shiprocketAuthenticate,
|
|
14
|
+
* shiprocketCreateOrder,
|
|
15
|
+
* shiprocketTrackByAWB,
|
|
16
|
+
* } from "@mohasinac/shipping-shiprocket";
|
|
17
|
+
*
|
|
18
|
+
* const { token } = await shiprocketAuthenticate({ email, password });
|
|
19
|
+
* const order = await shiprocketCreateOrder(token, orderData);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface ShiprocketAuthRequest {
|
|
23
|
+
email: string;
|
|
24
|
+
password: string;
|
|
25
|
+
}
|
|
26
|
+
interface ShiprocketAuthResponse {
|
|
27
|
+
token: string;
|
|
28
|
+
message: string;
|
|
29
|
+
data?: {
|
|
30
|
+
id: number;
|
|
31
|
+
email: string;
|
|
32
|
+
name: string;
|
|
33
|
+
company_id: number;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface ShiprocketPickupLocation {
|
|
37
|
+
id: number;
|
|
38
|
+
pickup_location: string;
|
|
39
|
+
add: string;
|
|
40
|
+
add2?: string;
|
|
41
|
+
city: string;
|
|
42
|
+
state: string;
|
|
43
|
+
country: string;
|
|
44
|
+
pin_code: string;
|
|
45
|
+
phone: string;
|
|
46
|
+
email: string;
|
|
47
|
+
name: string;
|
|
48
|
+
phone_verified?: number;
|
|
49
|
+
}
|
|
50
|
+
interface ShiprocketPickupLocationsResponse {
|
|
51
|
+
shipping_address: ShiprocketPickupLocation[];
|
|
52
|
+
}
|
|
53
|
+
interface ShiprocketAddPickupRequest {
|
|
54
|
+
pickup_location: string;
|
|
55
|
+
name: string;
|
|
56
|
+
email: string;
|
|
57
|
+
phone: string;
|
|
58
|
+
address: string;
|
|
59
|
+
address_2?: string;
|
|
60
|
+
city: string;
|
|
61
|
+
state: string;
|
|
62
|
+
country: string;
|
|
63
|
+
pin_code: string;
|
|
64
|
+
}
|
|
65
|
+
interface ShiprocketAddPickupResponse {
|
|
66
|
+
success: boolean;
|
|
67
|
+
address?: {
|
|
68
|
+
pickup_location_id: number;
|
|
69
|
+
name: string;
|
|
70
|
+
phone: string;
|
|
71
|
+
address: string;
|
|
72
|
+
city: string;
|
|
73
|
+
state: string;
|
|
74
|
+
country: string;
|
|
75
|
+
pin_code: string;
|
|
76
|
+
};
|
|
77
|
+
message?: string;
|
|
78
|
+
}
|
|
79
|
+
interface ShiprocketVerifyPickupOTPRequest {
|
|
80
|
+
otp: number;
|
|
81
|
+
pickup_location_id: number;
|
|
82
|
+
}
|
|
83
|
+
interface ShiprocketVerifyPickupOTPResponse {
|
|
84
|
+
success: boolean;
|
|
85
|
+
message: string;
|
|
86
|
+
}
|
|
87
|
+
interface ShiprocketOrderItem {
|
|
88
|
+
name: string;
|
|
89
|
+
sku: string;
|
|
90
|
+
units: number;
|
|
91
|
+
selling_price: number;
|
|
92
|
+
discount?: number;
|
|
93
|
+
tax?: string;
|
|
94
|
+
hsn?: number;
|
|
95
|
+
}
|
|
96
|
+
interface ShiprocketCreateOrderRequest {
|
|
97
|
+
order_id: string;
|
|
98
|
+
order_date: string;
|
|
99
|
+
pickup_location: string;
|
|
100
|
+
billing_customer_name: string;
|
|
101
|
+
billing_last_name?: string;
|
|
102
|
+
billing_address: string;
|
|
103
|
+
billing_address_2?: string;
|
|
104
|
+
billing_city: string;
|
|
105
|
+
billing_pincode: string;
|
|
106
|
+
billing_state: string;
|
|
107
|
+
billing_country: string;
|
|
108
|
+
billing_email: string;
|
|
109
|
+
billing_phone: string;
|
|
110
|
+
shipping_is_billing: boolean;
|
|
111
|
+
shipping_customer_name?: string;
|
|
112
|
+
shipping_last_name?: string;
|
|
113
|
+
shipping_address?: string;
|
|
114
|
+
shipping_address_2?: string;
|
|
115
|
+
shipping_city?: string;
|
|
116
|
+
shipping_pincode?: string;
|
|
117
|
+
shipping_state?: string;
|
|
118
|
+
shipping_country?: string;
|
|
119
|
+
shipping_phone?: string;
|
|
120
|
+
order_items: ShiprocketOrderItem[];
|
|
121
|
+
payment_method: "Prepaid" | "COD";
|
|
122
|
+
sub_total: number;
|
|
123
|
+
length: number;
|
|
124
|
+
breadth: number;
|
|
125
|
+
height: number;
|
|
126
|
+
weight: number;
|
|
127
|
+
}
|
|
128
|
+
interface ShiprocketCreateOrderResponse {
|
|
129
|
+
order_id: number;
|
|
130
|
+
shipment_id: number;
|
|
131
|
+
status: string;
|
|
132
|
+
status_code: number;
|
|
133
|
+
onboarding_completed_now: boolean;
|
|
134
|
+
awb_code?: string;
|
|
135
|
+
courier_company_id?: number;
|
|
136
|
+
courier_name?: string;
|
|
137
|
+
}
|
|
138
|
+
interface ShiprocketGenerateAWBRequest {
|
|
139
|
+
shipment_id: number;
|
|
140
|
+
courier_id?: number;
|
|
141
|
+
}
|
|
142
|
+
interface ShiprocketAWBResponse {
|
|
143
|
+
awb_code: string;
|
|
144
|
+
courier_company_id: number;
|
|
145
|
+
courier_name: string;
|
|
146
|
+
shipment_id: number;
|
|
147
|
+
assigned_date_time: string;
|
|
148
|
+
expected_delivery: string;
|
|
149
|
+
tracking_url?: string;
|
|
150
|
+
}
|
|
151
|
+
interface ShiprocketGeneratePickupRequest {
|
|
152
|
+
shipment_id: number[];
|
|
153
|
+
pickup_date?: string[];
|
|
154
|
+
}
|
|
155
|
+
interface ShiprocketPickupResponse {
|
|
156
|
+
pickup_scheduled_date?: string;
|
|
157
|
+
pickup_token_number?: string;
|
|
158
|
+
status: number;
|
|
159
|
+
others?: string;
|
|
160
|
+
}
|
|
161
|
+
interface ShiprocketTrackActivity {
|
|
162
|
+
date: string;
|
|
163
|
+
status: string;
|
|
164
|
+
activity: string;
|
|
165
|
+
location: string;
|
|
166
|
+
sr_status?: string;
|
|
167
|
+
sr_status_label?: string;
|
|
168
|
+
}
|
|
169
|
+
interface ShiprocketShipmentTrack {
|
|
170
|
+
id: number;
|
|
171
|
+
awb_code: string;
|
|
172
|
+
courier_company_id: number;
|
|
173
|
+
shipment_id: number;
|
|
174
|
+
order_id: number;
|
|
175
|
+
pickup_date?: string;
|
|
176
|
+
delivered_date?: string;
|
|
177
|
+
weight: string;
|
|
178
|
+
current_status: string;
|
|
179
|
+
edd?: string;
|
|
180
|
+
shipment_track_activities: ShiprocketTrackActivity[];
|
|
181
|
+
}
|
|
182
|
+
interface ShiprocketTrackingResponse {
|
|
183
|
+
tracking_data: {
|
|
184
|
+
awb_track_url: string;
|
|
185
|
+
track_url: string;
|
|
186
|
+
current_status: string;
|
|
187
|
+
current_status_id: number;
|
|
188
|
+
shipment_status: number;
|
|
189
|
+
shipment_track: ShiprocketShipmentTrack[];
|
|
190
|
+
shipment_track_activities: ShiprocketTrackActivity[];
|
|
191
|
+
error?: string;
|
|
192
|
+
cod?: boolean;
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
interface ShiprocketCourierServiceabilityResponse {
|
|
196
|
+
data?: {
|
|
197
|
+
available_courier_companies: Array<{
|
|
198
|
+
id: number;
|
|
199
|
+
name: string;
|
|
200
|
+
etd: string;
|
|
201
|
+
rate: number;
|
|
202
|
+
cod_charges?: number;
|
|
203
|
+
}>;
|
|
204
|
+
};
|
|
205
|
+
status: number;
|
|
206
|
+
message?: string;
|
|
207
|
+
}
|
|
208
|
+
interface ShiprocketWebhookPayload {
|
|
209
|
+
awb: string;
|
|
210
|
+
order_id: string;
|
|
211
|
+
shipment_id: string;
|
|
212
|
+
current_status: string;
|
|
213
|
+
current_status_id: number;
|
|
214
|
+
courier_agent_assigned?: boolean;
|
|
215
|
+
etd?: string;
|
|
216
|
+
pickup_generated?: boolean;
|
|
217
|
+
}
|
|
218
|
+
declare function shiprocketAuthenticate(credentials: ShiprocketAuthRequest): Promise<ShiprocketAuthResponse>;
|
|
219
|
+
declare function shiprocketGetPickupLocations(token: string): Promise<ShiprocketPickupLocationsResponse>;
|
|
220
|
+
declare function shiprocketAddPickupLocation(token: string, data: ShiprocketAddPickupRequest): Promise<ShiprocketAddPickupResponse>;
|
|
221
|
+
declare function shiprocketVerifyPickupOTP(token: string, data: ShiprocketVerifyPickupOTPRequest): Promise<ShiprocketVerifyPickupOTPResponse>;
|
|
222
|
+
declare function shiprocketCreateOrder(token: string, order: ShiprocketCreateOrderRequest): Promise<ShiprocketCreateOrderResponse>;
|
|
223
|
+
declare function shiprocketGenerateAWB(token: string, params: ShiprocketGenerateAWBRequest): Promise<ShiprocketAWBResponse>;
|
|
224
|
+
declare function shiprocketGeneratePickup(token: string, params: ShiprocketGeneratePickupRequest): Promise<ShiprocketPickupResponse>;
|
|
225
|
+
declare function shiprocketTrackByAWB(token: string, awb: string): Promise<ShiprocketTrackingResponse>;
|
|
226
|
+
declare function shiprocketCheckServiceability(token: string, paramsOrPickup: {
|
|
227
|
+
pickup_postcode: string;
|
|
228
|
+
delivery_postcode: string;
|
|
229
|
+
weight: number;
|
|
230
|
+
cod?: 0 | 1;
|
|
231
|
+
} | string, deliveryPostcode?: string, weight?: number, cod?: boolean): Promise<ShiprocketCourierServiceabilityResponse>;
|
|
232
|
+
/** Shiprocket tokens are valid for approx. 10 days */
|
|
233
|
+
declare const SHIPROCKET_TOKEN_TTL_MS: number;
|
|
234
|
+
declare function isShiprocketTokenExpired(expiry: Date | undefined): boolean;
|
|
235
|
+
|
|
236
|
+
export { SHIPROCKET_TOKEN_TTL_MS, type ShiprocketAWBResponse, type ShiprocketAddPickupRequest, type ShiprocketAddPickupResponse, type ShiprocketAuthRequest, type ShiprocketAuthResponse, type ShiprocketCourierServiceabilityResponse, type ShiprocketCreateOrderRequest, type ShiprocketCreateOrderResponse, type ShiprocketGenerateAWBRequest, type ShiprocketGeneratePickupRequest, type ShiprocketOrderItem, type ShiprocketPickupLocation, type ShiprocketPickupLocationsResponse, type ShiprocketPickupResponse, type ShiprocketShipmentTrack, type ShiprocketTrackActivity, type ShiprocketTrackingResponse, type ShiprocketVerifyPickupOTPRequest, type ShiprocketVerifyPickupOTPResponse, type ShiprocketWebhookPayload, isShiprocketTokenExpired, shiprocketAddPickupLocation, shiprocketAuthenticate, shiprocketCheckServiceability, shiprocketCreateOrder, shiprocketGenerateAWB, shiprocketGeneratePickup, shiprocketGetPickupLocations, shiprocketTrackByAWB, shiprocketVerifyPickupOTP };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @mohasinac/shipping-shiprocket — Shiprocket shipping API client
|
|
3
|
+
*
|
|
4
|
+
* Thin HTTP client for the Shiprocket REST API.
|
|
5
|
+
* No credential management — caller is responsible for obtaining and
|
|
6
|
+
* passing the JWT token (see your project's platform auth layer).
|
|
7
|
+
*
|
|
8
|
+
* API base: https://apiv2.shiprocket.in/v1/external
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import {
|
|
13
|
+
* shiprocketAuthenticate,
|
|
14
|
+
* shiprocketCreateOrder,
|
|
15
|
+
* shiprocketTrackByAWB,
|
|
16
|
+
* } from "@mohasinac/shipping-shiprocket";
|
|
17
|
+
*
|
|
18
|
+
* const { token } = await shiprocketAuthenticate({ email, password });
|
|
19
|
+
* const order = await shiprocketCreateOrder(token, orderData);
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface ShiprocketAuthRequest {
|
|
23
|
+
email: string;
|
|
24
|
+
password: string;
|
|
25
|
+
}
|
|
26
|
+
interface ShiprocketAuthResponse {
|
|
27
|
+
token: string;
|
|
28
|
+
message: string;
|
|
29
|
+
data?: {
|
|
30
|
+
id: number;
|
|
31
|
+
email: string;
|
|
32
|
+
name: string;
|
|
33
|
+
company_id: number;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
interface ShiprocketPickupLocation {
|
|
37
|
+
id: number;
|
|
38
|
+
pickup_location: string;
|
|
39
|
+
add: string;
|
|
40
|
+
add2?: string;
|
|
41
|
+
city: string;
|
|
42
|
+
state: string;
|
|
43
|
+
country: string;
|
|
44
|
+
pin_code: string;
|
|
45
|
+
phone: string;
|
|
46
|
+
email: string;
|
|
47
|
+
name: string;
|
|
48
|
+
phone_verified?: number;
|
|
49
|
+
}
|
|
50
|
+
interface ShiprocketPickupLocationsResponse {
|
|
51
|
+
shipping_address: ShiprocketPickupLocation[];
|
|
52
|
+
}
|
|
53
|
+
interface ShiprocketAddPickupRequest {
|
|
54
|
+
pickup_location: string;
|
|
55
|
+
name: string;
|
|
56
|
+
email: string;
|
|
57
|
+
phone: string;
|
|
58
|
+
address: string;
|
|
59
|
+
address_2?: string;
|
|
60
|
+
city: string;
|
|
61
|
+
state: string;
|
|
62
|
+
country: string;
|
|
63
|
+
pin_code: string;
|
|
64
|
+
}
|
|
65
|
+
interface ShiprocketAddPickupResponse {
|
|
66
|
+
success: boolean;
|
|
67
|
+
address?: {
|
|
68
|
+
pickup_location_id: number;
|
|
69
|
+
name: string;
|
|
70
|
+
phone: string;
|
|
71
|
+
address: string;
|
|
72
|
+
city: string;
|
|
73
|
+
state: string;
|
|
74
|
+
country: string;
|
|
75
|
+
pin_code: string;
|
|
76
|
+
};
|
|
77
|
+
message?: string;
|
|
78
|
+
}
|
|
79
|
+
interface ShiprocketVerifyPickupOTPRequest {
|
|
80
|
+
otp: number;
|
|
81
|
+
pickup_location_id: number;
|
|
82
|
+
}
|
|
83
|
+
interface ShiprocketVerifyPickupOTPResponse {
|
|
84
|
+
success: boolean;
|
|
85
|
+
message: string;
|
|
86
|
+
}
|
|
87
|
+
interface ShiprocketOrderItem {
|
|
88
|
+
name: string;
|
|
89
|
+
sku: string;
|
|
90
|
+
units: number;
|
|
91
|
+
selling_price: number;
|
|
92
|
+
discount?: number;
|
|
93
|
+
tax?: string;
|
|
94
|
+
hsn?: number;
|
|
95
|
+
}
|
|
96
|
+
interface ShiprocketCreateOrderRequest {
|
|
97
|
+
order_id: string;
|
|
98
|
+
order_date: string;
|
|
99
|
+
pickup_location: string;
|
|
100
|
+
billing_customer_name: string;
|
|
101
|
+
billing_last_name?: string;
|
|
102
|
+
billing_address: string;
|
|
103
|
+
billing_address_2?: string;
|
|
104
|
+
billing_city: string;
|
|
105
|
+
billing_pincode: string;
|
|
106
|
+
billing_state: string;
|
|
107
|
+
billing_country: string;
|
|
108
|
+
billing_email: string;
|
|
109
|
+
billing_phone: string;
|
|
110
|
+
shipping_is_billing: boolean;
|
|
111
|
+
shipping_customer_name?: string;
|
|
112
|
+
shipping_last_name?: string;
|
|
113
|
+
shipping_address?: string;
|
|
114
|
+
shipping_address_2?: string;
|
|
115
|
+
shipping_city?: string;
|
|
116
|
+
shipping_pincode?: string;
|
|
117
|
+
shipping_state?: string;
|
|
118
|
+
shipping_country?: string;
|
|
119
|
+
shipping_phone?: string;
|
|
120
|
+
order_items: ShiprocketOrderItem[];
|
|
121
|
+
payment_method: "Prepaid" | "COD";
|
|
122
|
+
sub_total: number;
|
|
123
|
+
length: number;
|
|
124
|
+
breadth: number;
|
|
125
|
+
height: number;
|
|
126
|
+
weight: number;
|
|
127
|
+
}
|
|
128
|
+
interface ShiprocketCreateOrderResponse {
|
|
129
|
+
order_id: number;
|
|
130
|
+
shipment_id: number;
|
|
131
|
+
status: string;
|
|
132
|
+
status_code: number;
|
|
133
|
+
onboarding_completed_now: boolean;
|
|
134
|
+
awb_code?: string;
|
|
135
|
+
courier_company_id?: number;
|
|
136
|
+
courier_name?: string;
|
|
137
|
+
}
|
|
138
|
+
interface ShiprocketGenerateAWBRequest {
|
|
139
|
+
shipment_id: number;
|
|
140
|
+
courier_id?: number;
|
|
141
|
+
}
|
|
142
|
+
interface ShiprocketAWBResponse {
|
|
143
|
+
awb_code: string;
|
|
144
|
+
courier_company_id: number;
|
|
145
|
+
courier_name: string;
|
|
146
|
+
shipment_id: number;
|
|
147
|
+
assigned_date_time: string;
|
|
148
|
+
expected_delivery: string;
|
|
149
|
+
tracking_url?: string;
|
|
150
|
+
}
|
|
151
|
+
interface ShiprocketGeneratePickupRequest {
|
|
152
|
+
shipment_id: number[];
|
|
153
|
+
pickup_date?: string[];
|
|
154
|
+
}
|
|
155
|
+
interface ShiprocketPickupResponse {
|
|
156
|
+
pickup_scheduled_date?: string;
|
|
157
|
+
pickup_token_number?: string;
|
|
158
|
+
status: number;
|
|
159
|
+
others?: string;
|
|
160
|
+
}
|
|
161
|
+
interface ShiprocketTrackActivity {
|
|
162
|
+
date: string;
|
|
163
|
+
status: string;
|
|
164
|
+
activity: string;
|
|
165
|
+
location: string;
|
|
166
|
+
sr_status?: string;
|
|
167
|
+
sr_status_label?: string;
|
|
168
|
+
}
|
|
169
|
+
interface ShiprocketShipmentTrack {
|
|
170
|
+
id: number;
|
|
171
|
+
awb_code: string;
|
|
172
|
+
courier_company_id: number;
|
|
173
|
+
shipment_id: number;
|
|
174
|
+
order_id: number;
|
|
175
|
+
pickup_date?: string;
|
|
176
|
+
delivered_date?: string;
|
|
177
|
+
weight: string;
|
|
178
|
+
current_status: string;
|
|
179
|
+
edd?: string;
|
|
180
|
+
shipment_track_activities: ShiprocketTrackActivity[];
|
|
181
|
+
}
|
|
182
|
+
interface ShiprocketTrackingResponse {
|
|
183
|
+
tracking_data: {
|
|
184
|
+
awb_track_url: string;
|
|
185
|
+
track_url: string;
|
|
186
|
+
current_status: string;
|
|
187
|
+
current_status_id: number;
|
|
188
|
+
shipment_status: number;
|
|
189
|
+
shipment_track: ShiprocketShipmentTrack[];
|
|
190
|
+
shipment_track_activities: ShiprocketTrackActivity[];
|
|
191
|
+
error?: string;
|
|
192
|
+
cod?: boolean;
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
interface ShiprocketCourierServiceabilityResponse {
|
|
196
|
+
data?: {
|
|
197
|
+
available_courier_companies: Array<{
|
|
198
|
+
id: number;
|
|
199
|
+
name: string;
|
|
200
|
+
etd: string;
|
|
201
|
+
rate: number;
|
|
202
|
+
cod_charges?: number;
|
|
203
|
+
}>;
|
|
204
|
+
};
|
|
205
|
+
status: number;
|
|
206
|
+
message?: string;
|
|
207
|
+
}
|
|
208
|
+
interface ShiprocketWebhookPayload {
|
|
209
|
+
awb: string;
|
|
210
|
+
order_id: string;
|
|
211
|
+
shipment_id: string;
|
|
212
|
+
current_status: string;
|
|
213
|
+
current_status_id: number;
|
|
214
|
+
courier_agent_assigned?: boolean;
|
|
215
|
+
etd?: string;
|
|
216
|
+
pickup_generated?: boolean;
|
|
217
|
+
}
|
|
218
|
+
declare function shiprocketAuthenticate(credentials: ShiprocketAuthRequest): Promise<ShiprocketAuthResponse>;
|
|
219
|
+
declare function shiprocketGetPickupLocations(token: string): Promise<ShiprocketPickupLocationsResponse>;
|
|
220
|
+
declare function shiprocketAddPickupLocation(token: string, data: ShiprocketAddPickupRequest): Promise<ShiprocketAddPickupResponse>;
|
|
221
|
+
declare function shiprocketVerifyPickupOTP(token: string, data: ShiprocketVerifyPickupOTPRequest): Promise<ShiprocketVerifyPickupOTPResponse>;
|
|
222
|
+
declare function shiprocketCreateOrder(token: string, order: ShiprocketCreateOrderRequest): Promise<ShiprocketCreateOrderResponse>;
|
|
223
|
+
declare function shiprocketGenerateAWB(token: string, params: ShiprocketGenerateAWBRequest): Promise<ShiprocketAWBResponse>;
|
|
224
|
+
declare function shiprocketGeneratePickup(token: string, params: ShiprocketGeneratePickupRequest): Promise<ShiprocketPickupResponse>;
|
|
225
|
+
declare function shiprocketTrackByAWB(token: string, awb: string): Promise<ShiprocketTrackingResponse>;
|
|
226
|
+
declare function shiprocketCheckServiceability(token: string, paramsOrPickup: {
|
|
227
|
+
pickup_postcode: string;
|
|
228
|
+
delivery_postcode: string;
|
|
229
|
+
weight: number;
|
|
230
|
+
cod?: 0 | 1;
|
|
231
|
+
} | string, deliveryPostcode?: string, weight?: number, cod?: boolean): Promise<ShiprocketCourierServiceabilityResponse>;
|
|
232
|
+
/** Shiprocket tokens are valid for approx. 10 days */
|
|
233
|
+
declare const SHIPROCKET_TOKEN_TTL_MS: number;
|
|
234
|
+
declare function isShiprocketTokenExpired(expiry: Date | undefined): boolean;
|
|
235
|
+
|
|
236
|
+
export { SHIPROCKET_TOKEN_TTL_MS, type ShiprocketAWBResponse, type ShiprocketAddPickupRequest, type ShiprocketAddPickupResponse, type ShiprocketAuthRequest, type ShiprocketAuthResponse, type ShiprocketCourierServiceabilityResponse, type ShiprocketCreateOrderRequest, type ShiprocketCreateOrderResponse, type ShiprocketGenerateAWBRequest, type ShiprocketGeneratePickupRequest, type ShiprocketOrderItem, type ShiprocketPickupLocation, type ShiprocketPickupLocationsResponse, type ShiprocketPickupResponse, type ShiprocketShipmentTrack, type ShiprocketTrackActivity, type ShiprocketTrackingResponse, type ShiprocketVerifyPickupOTPRequest, type ShiprocketVerifyPickupOTPResponse, type ShiprocketWebhookPayload, isShiprocketTokenExpired, shiprocketAddPickupLocation, shiprocketAuthenticate, shiprocketCheckServiceability, shiprocketCreateOrder, shiprocketGenerateAWB, shiprocketGeneratePickup, shiprocketGetPickupLocations, shiprocketTrackByAWB, shiprocketVerifyPickupOTP };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/index.ts
|
|
34
|
+
var BASE_URL = "https://apiv2.shiprocket.in/v1/external";
|
|
35
|
+
async function shiprocketFetch(endpoint, options = {}) {
|
|
36
|
+
var _b;
|
|
37
|
+
const _a = options, { token, headers = {} } = _a, rest = __objRest(_a, ["token", "headers"]);
|
|
38
|
+
const reqHeaders = __spreadValues({
|
|
39
|
+
"Content-Type": "application/json"
|
|
40
|
+
}, headers);
|
|
41
|
+
if (token) reqHeaders["Authorization"] = `Bearer ${token}`;
|
|
42
|
+
const response = await fetch(`${BASE_URL}${endpoint}`, __spreadProps(__spreadValues({}, rest), {
|
|
43
|
+
headers: reqHeaders
|
|
44
|
+
}));
|
|
45
|
+
const text = await response.text();
|
|
46
|
+
let json;
|
|
47
|
+
try {
|
|
48
|
+
json = JSON.parse(text);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Shiprocket API non-JSON response ${response.status}: ${text.slice(0, 200)}`
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const message = (_b = json == null ? void 0 : json.message) != null ? _b : `Shiprocket API error ${response.status}`;
|
|
56
|
+
throw new Error(message);
|
|
57
|
+
}
|
|
58
|
+
return json;
|
|
59
|
+
}
|
|
60
|
+
async function shiprocketAuthenticate(credentials) {
|
|
61
|
+
return shiprocketFetch("/auth/login", {
|
|
62
|
+
method: "POST",
|
|
63
|
+
body: JSON.stringify(credentials)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async function shiprocketGetPickupLocations(token) {
|
|
67
|
+
return shiprocketFetch(
|
|
68
|
+
"/settings/company/pickup",
|
|
69
|
+
{ token }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
async function shiprocketAddPickupLocation(token, data) {
|
|
73
|
+
return shiprocketFetch(
|
|
74
|
+
"/settings/company/addpickup",
|
|
75
|
+
{ method: "POST", token, body: JSON.stringify(data) }
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
async function shiprocketVerifyPickupOTP(token, data) {
|
|
79
|
+
return shiprocketFetch(
|
|
80
|
+
"/settings/company/verifyOtpForPickup",
|
|
81
|
+
{ method: "POST", token, body: JSON.stringify(data) }
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
async function shiprocketCreateOrder(token, order) {
|
|
85
|
+
return shiprocketFetch(
|
|
86
|
+
"/orders/create/adhoc",
|
|
87
|
+
{ method: "POST", token, body: JSON.stringify(order) }
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
async function shiprocketGenerateAWB(token, params) {
|
|
91
|
+
return shiprocketFetch("/courier/assign/awb", {
|
|
92
|
+
method: "POST",
|
|
93
|
+
token,
|
|
94
|
+
body: JSON.stringify(params)
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async function shiprocketGeneratePickup(token, params) {
|
|
98
|
+
return shiprocketFetch("/courier/generate/pickup", {
|
|
99
|
+
method: "POST",
|
|
100
|
+
token,
|
|
101
|
+
body: JSON.stringify(params)
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async function shiprocketTrackByAWB(token, awb) {
|
|
105
|
+
return shiprocketFetch(
|
|
106
|
+
`/courier/track/awb/${awb}`,
|
|
107
|
+
{ token }
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
async function shiprocketCheckServiceability(token, paramsOrPickup, deliveryPostcode, weight, cod = false) {
|
|
111
|
+
const params = typeof paramsOrPickup === "string" ? {
|
|
112
|
+
pickup_postcode: paramsOrPickup,
|
|
113
|
+
delivery_postcode: deliveryPostcode != null ? deliveryPostcode : "",
|
|
114
|
+
weight: weight != null ? weight : 0,
|
|
115
|
+
cod: cod ? 1 : 0
|
|
116
|
+
} : paramsOrPickup;
|
|
117
|
+
const sp = new URLSearchParams();
|
|
118
|
+
sp.set("pickup_postcode", params.pickup_postcode);
|
|
119
|
+
sp.set("delivery_postcode", params.delivery_postcode);
|
|
120
|
+
sp.set("weight", String(params.weight));
|
|
121
|
+
if (params.cod !== void 0) sp.set("cod", String(params.cod));
|
|
122
|
+
return shiprocketFetch(
|
|
123
|
+
`/courier/serviceability/?${sp.toString()}`,
|
|
124
|
+
{ token }
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
var SHIPROCKET_TOKEN_TTL_MS = 9 * 24 * 60 * 60 * 1e3;
|
|
128
|
+
function isShiprocketTokenExpired(expiry) {
|
|
129
|
+
if (!expiry) return true;
|
|
130
|
+
return /* @__PURE__ */ new Date() >= new Date(expiry);
|
|
131
|
+
}
|
|
132
|
+
export {
|
|
133
|
+
SHIPROCKET_TOKEN_TTL_MS,
|
|
134
|
+
isShiprocketTokenExpired,
|
|
135
|
+
shiprocketAddPickupLocation,
|
|
136
|
+
shiprocketAuthenticate,
|
|
137
|
+
shiprocketCheckServiceability,
|
|
138
|
+
shiprocketCreateOrder,
|
|
139
|
+
shiprocketGenerateAWB,
|
|
140
|
+
shiprocketGeneratePickup,
|
|
141
|
+
shiprocketGetPickupLocations,
|
|
142
|
+
shiprocketTrackByAWB,
|
|
143
|
+
shiprocketVerifyPickupOTP
|
|
144
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mohasinac/shipping-shiprocket",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": { "access": "public" },
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js",
|
|
14
|
+
"require": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": ["dist"],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
20
|
+
"dev": "tsup --watch",
|
|
21
|
+
"typecheck": "tsc --noEmit"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@mohasinac/contracts": "^1.1.0",
|
|
25
|
+
"@mohasinac/errors": "^1.1.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"tsup": "^8.5.0",
|
|
29
|
+
"typescript": "^5.9.3"
|
|
30
|
+
}
|
|
31
|
+
}
|