Skip to content

Commit 42f157f

Browse files
Merge pull request #24 from AfterShip-Team-Mocha/2025-01
feat: supported 2025-01 version.
2 parents bd716b5 + 7eb32ce commit 42f157f

File tree

151 files changed

+7098
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+7098
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2025 AfterShip
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 285 additions & 1 deletion
Large diffs are not rendered by default.

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@aftership/tracking-sdk",
3+
"version": "12.0.0",
4+
"description": "Tracking NodeJS SDK",
5+
"main": "dist/aftership.js",
6+
"types": "dist/aftership.d.ts",
7+
"engines": {
8+
"node": ">=16.20.0"
9+
},
10+
"scripts": {
11+
"build": "npm run build:clean && tsc",
12+
"build:clean": "rm -rf ./dist ./coverage",
13+
"test": "echo \"Error: no test specified\" && exit 1"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/aftership/tracking-sdk-nodejs"
18+
},
19+
"author": "",
20+
"license": "ISC",
21+
"bugs": {
22+
"url": "https://github.com/AfterShip/tracking-sdk-nodejs/issues"
23+
},
24+
"homepage": "https://github.com/aftership/tracking-sdk-nodejs/README.md",
25+
"dependencies": {
26+
"axios": "^1.7.2"
27+
},
28+
"keywords": [
29+
"aftership",
30+
"tracking"
31+
],
32+
"devDependencies": {
33+
"@types/node": "^20.11.25",
34+
"typescript": "^4.9.5"
35+
}
36+
}

src/aftership.ts

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* This code was auto generated by AfterShip SDK Generator.
3+
* Do not edit the class manually.
4+
*/
5+
import { TrackingApi } from "./api/Tracking";
6+
import { EstimatedDeliveryDateApi } from "./api/EstimatedDeliveryDate";
7+
import { CourierApi } from "./api/Courier";
8+
import { AftershipError, AfterShipErrorCodes } from "./error";
9+
import { AuthType } from "./lib/authentication";
10+
import {
11+
DEFAULT_DOMAIN,
12+
DEFAULT_MAX_RETRY,
13+
DEFAULT_TIMEOUT,
14+
DEFAULT_USER_AGENT,
15+
MAX_MAX_RETRY,
16+
MIN_MAX_RETRY,
17+
Request,
18+
} from "./lib/request";
19+
import { parseProxy } from "./utils/parse_proxy";
20+
21+
export interface Options {
22+
auth_type?: AuthType;
23+
api_key?: string;
24+
api_secret?: string;
25+
domain?: string;
26+
max_retry?: number;
27+
timeout?: number;
28+
user_agent?: string;
29+
proxy?: string;
30+
}
31+
32+
const SDK_ENV_PREFIX = "AFTERSHIP_TRACKING_SDK";
33+
34+
export class AfterShip {
35+
public readonly tracking: TrackingApi;
36+
public readonly estimatedDeliveryDate: EstimatedDeliveryDateApi;
37+
public readonly courier: CourierApi;
38+
private readonly options: Options;
39+
40+
constructor(options?: Options) {
41+
this.options = options === undefined ? {} : options;
42+
43+
if (this.options.api_key === undefined) {
44+
this.options.api_key = process.env[`${SDK_ENV_PREFIX}_API_KEY`];
45+
}
46+
if (this.options.api_secret === undefined) {
47+
this.options.api_secret = process.env[`${SDK_ENV_PREFIX}_API_SECRET`];
48+
}
49+
if (this.options.user_agent === undefined) {
50+
this.options.user_agent = process.env[`${SDK_ENV_PREFIX}_USER_AGENT`];
51+
if (this.options.user_agent === undefined) {
52+
this.options.user_agent = DEFAULT_USER_AGENT;
53+
}
54+
}
55+
if (this.options.auth_type === undefined) {
56+
this.options.auth_type = process.env[
57+
`${SDK_ENV_PREFIX}_AUTHENTICATION_TYPE`
58+
] as AuthType;
59+
if (this.options.auth_type === undefined) {
60+
this.options.auth_type = AuthType.API_KEY;
61+
}
62+
}
63+
if (this.options.domain === undefined) {
64+
this.options.domain = process.env[`${SDK_ENV_PREFIX}_DOMAIN`];
65+
if (this.options.domain === undefined) {
66+
this.options.domain = DEFAULT_DOMAIN;
67+
}
68+
}
69+
if (this.options.proxy === undefined) {
70+
this.options.proxy = process.env[`${SDK_ENV_PREFIX}_PROXY`];
71+
}
72+
if (this.options.max_retry === undefined) {
73+
const env_max_retry = process.env[`${SDK_ENV_PREFIX}_MAX_RETRY`];
74+
if (env_max_retry === undefined) {
75+
this.options.max_retry = DEFAULT_MAX_RETRY;
76+
} else {
77+
this.options.max_retry = parseInt(env_max_retry);
78+
}
79+
}
80+
if (this.options.timeout === undefined) {
81+
const env_timeout = process.env[`${SDK_ENV_PREFIX}_TIMEOUT`];
82+
if (env_timeout === undefined) {
83+
this.options.timeout = DEFAULT_TIMEOUT;
84+
} else {
85+
this.options.timeout = parseInt(env_timeout);
86+
}
87+
}
88+
89+
this.validateOptions();
90+
91+
const request = new Request({
92+
auth_type: this.options.auth_type,
93+
api_key: this.options.api_key,
94+
api_secret: this.options.api_secret,
95+
domain: this.options.domain,
96+
max_retry: this.options.max_retry,
97+
timeout: this.options.timeout,
98+
user_agent: this.options.user_agent,
99+
proxy: parseProxy(this.options.proxy),
100+
});
101+
this.tracking = new TrackingApi(request);
102+
this.estimatedDeliveryDate = new EstimatedDeliveryDateApi(request);
103+
this.courier = new CourierApi(request);
104+
}
105+
106+
private validateOptions() {
107+
if (this.options.api_key === undefined || this.options.api_key === "") {
108+
throw new AftershipError(
109+
"Invalid API key",
110+
AfterShipErrorCodes.INVALID_API_KEY,
111+
);
112+
}
113+
if (
114+
(this.options.auth_type === AuthType.AES ||
115+
this.options.auth_type === AuthType.RSA) &&
116+
(this.options.api_secret === undefined || this.options.api_secret === "")
117+
) {
118+
throw new AftershipError(
119+
`Invalid option: auth_type`,
120+
AfterShipErrorCodes.INVALID_OPTION,
121+
);
122+
}
123+
if (
124+
this.options.domain &&
125+
((this.options.domain.indexOf("http") < 0 &&
126+
this.options.domain.indexOf("https") < 0) ||
127+
this.options.domain.endsWith("/"))
128+
) {
129+
throw new AftershipError(
130+
`Invalid option: domain`,
131+
AfterShipErrorCodes.INVALID_OPTION,
132+
);
133+
}
134+
if (
135+
this.options.max_retry !== undefined &&
136+
(isNaN(this.options.max_retry) ||
137+
this.options.max_retry > MAX_MAX_RETRY ||
138+
this.options.max_retry < MIN_MAX_RETRY)
139+
) {
140+
throw new AftershipError(
141+
`Invalid option: max_retry`,
142+
AfterShipErrorCodes.INVALID_OPTION,
143+
);
144+
}
145+
if (
146+
this.options.timeout !== undefined &&
147+
(isNaN(this.options.timeout) || this.options.timeout < 0)
148+
) {
149+
throw new AftershipError(
150+
`Invalid option: timeout`,
151+
AfterShipErrorCodes.INVALID_OPTION,
152+
);
153+
}
154+
if (this.options.proxy !== undefined && this.options.proxy.length === 0) {
155+
throw new AftershipError(
156+
`Invalid option: proxy`,
157+
AfterShipErrorCodes.INVALID_OPTION,
158+
);
159+
}
160+
}
161+
}
162+
163+
export { AuthType, AfterShipErrorCodes, AftershipError };

src/api/Courier.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This code was auto generated by AfterShip SDK Generator.
3+
* Do not edit the class manually.
4+
*/
5+
import { Request } from "../lib/request";
6+
import { AftershipError, AfterShipErrorCodes } from "../error";
7+
import { GetUserCouriersResponse } from "../model/GetUserCouriersResponse";
8+
import { DetectCourierRequest } from "../model/DetectCourierRequest";
9+
import { DetectCourierResponse } from "../model/DetectCourierResponse";
10+
import { GetAllCouriersResponse } from "../model/GetAllCouriersResponse";
11+
12+
export class CourierApi {
13+
private readonly request: Request;
14+
15+
constructor(request: Request) {
16+
this.request = request;
17+
}
18+
19+
/**
20+
* Return a list of .
21+
*/
22+
public async getUserCouriers(headers?: {
23+
[key: string]: any;
24+
}): Promise<GetUserCouriersResponse> {
25+
return this.request.makeRequest<GetUserCouriersResponse>({
26+
url: `/tracking/2025-01/couriers`,
27+
method: "GET",
28+
headers,
29+
request_legacy_tag: "",
30+
response_legacy_tag: "",
31+
is_paging: false,
32+
});
33+
}
34+
/**
35+
* Return a list of matched couriers based on tracking number format and or a list of couriers.
36+
*/
37+
public async detectCourier(
38+
body: DetectCourierRequest,
39+
headers?: { [key: string]: any },
40+
): Promise<DetectCourierResponse> {
41+
return this.request.makeRequest<DetectCourierResponse>({
42+
url: `/tracking/2025-01/couriers/detect`,
43+
method: "POST",
44+
body,
45+
headers,
46+
request_legacy_tag: "",
47+
response_legacy_tag: "",
48+
is_paging: false,
49+
});
50+
}
51+
/**
52+
* Return a list of all couriers.
53+
*/
54+
public async getAllCouriers(headers?: {
55+
[key: string]: any;
56+
}): Promise<GetAllCouriersResponse> {
57+
return this.request.makeRequest<GetAllCouriersResponse>({
58+
url: `/tracking/2025-01/couriers/all`,
59+
method: "GET",
60+
headers,
61+
request_legacy_tag: "",
62+
response_legacy_tag: "",
63+
is_paging: false,
64+
});
65+
}
66+
}

src/api/EstimatedDeliveryDate.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This code was auto generated by AfterShip SDK Generator.
3+
* Do not edit the class manually.
4+
*/
5+
import { Request } from "../lib/request";
6+
import { AftershipError, AfterShipErrorCodes } from "../error";
7+
import { PredictRequest } from "../model/PredictRequest";
8+
import { PredictResponse } from "../model/PredictResponse";
9+
import { PredictBatchRequest } from "../model/PredictBatchRequest";
10+
import { PredictBatchResponse } from "../model/PredictBatchResponse";
11+
12+
export class EstimatedDeliveryDateApi {
13+
private readonly request: Request;
14+
15+
constructor(request: Request) {
16+
this.request = request;
17+
}
18+
19+
/**
20+
* &gt; The estimated delivery date is provided by AfterShip, based on its AI-predictive model. You can display the EDD on the product page, cart, and order checkout page. It indicates when a customer will receive the order.You can use to activate this feature.
21+
*/
22+
public async predict(
23+
body: PredictRequest,
24+
headers?: { [key: string]: any },
25+
): Promise<PredictResponse> {
26+
return this.request.makeRequest<PredictResponse>({
27+
url: `/tracking/2025-01/estimated-delivery-date/predict`,
28+
method: "POST",
29+
body,
30+
headers,
31+
request_legacy_tag: "",
32+
response_legacy_tag: "",
33+
is_paging: false,
34+
});
35+
}
36+
/**
37+
* &gt; The estimated delivery date is provided by AfterShip, based on its AI-predictive model. You can display the EDD on the product page, cart, and order checkout page. It indicates when a customer will receive the order.You can use to activate this feature.Supported functionalities require:1. One `EstimatedDeliveryDate` object for one prediction result.2. Maximum 5 `EstimatedDeliveryDate` objects are allowed.3. API call will fail if any of the requests `EstimatedDeliveryDate` objects do not meet the specification requirement.
38+
*/
39+
public async predictBatch(
40+
body: PredictBatchRequest,
41+
headers?: { [key: string]: any },
42+
): Promise<PredictBatchResponse> {
43+
return this.request.makeRequest<PredictBatchResponse>({
44+
url: `/tracking/2025-01/estimated-delivery-date/predict-batch`,
45+
method: "POST",
46+
body,
47+
headers,
48+
request_legacy_tag: "",
49+
response_legacy_tag: "",
50+
is_paging: false,
51+
});
52+
}
53+
}

0 commit comments

Comments
 (0)