Skip to content

Commit

Permalink
✨ can handle new rate-limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Meischner committed Nov 5, 2021
1 parent f590684 commit 93e1cab
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
/lib

.env
10 changes: 10 additions & 0 deletions examples.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Transactions API
GET https://app.wecantrack.com/api/v3/transactions?date_type=order_date&start_date=2021-09-21T00:00:00&end_date=2021-09-23T00:00:00
X-API-Key:{{ENV_WCT_KEY}}

# Aggregation API
GET https://app.wecantrack.com/api/v2/clicks/aggregation?last_clicks=10000&order_by=commissions&group_by=custom_index_2&limit=100
X-API-Key:{{ENV_WCT_KEY}}



48 changes: 23 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"typescript-tslint-plugin": "^1.0.1"
},
"dependencies": {
"node-fetch": "^2.6.0"
"node-fetch": "^3.0.0"
},
"files": [
"lib/**/*"
Expand Down
6 changes: 3 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import { TransactionData, TransactionRequest, TransactionResponse } from './tran
import { getAllPages, getArrayPage, getSinglePage } from './utils';

export class WeCanTrack {
constructor(private key: string) {}
constructor(private key: string) { }

async transactionsPage(request: TransactionRequest): Promise<TransactionResponse> {
return getSinglePage(request, Endpoint.TRANSACTIONS, this.key);
return getSinglePage(request, Endpoint.TRANSACTIONS, this.key)[0];
}

async transactionsTotal(request: TransactionRequest): Promise<TransactionData[]> {
return getAllPages(request, Endpoint.TRANSACTIONS, this.key);
}

async clickoutsPage(request: ClickoutRequest): Promise<ClickoutResponse> {
return getSinglePage(request, Endpoint.CLICKOUTS, this.key);
return getSinglePage(request, Endpoint.CLICKOUTS, this.key)[0];
}

async clickoutsTotal(request: ClickoutRequest): Promise<ClickoutData[]> {
Expand Down
3 changes: 1 addition & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export type WCTDate = `${number | string}-${number | string}-${number | string}T${number | string}:${number | string}:${
| number
export type WCTDate = `${number | string}-${number | string}-${number | string}T${number | string}:${number | string}:${| number
| string}`;

export type DateType = 'order_date' | 'modified_date' | 'click_date' | 'validation_date' | 'last_wct_update';
Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function getSinglePage<T extends GeneralResponseData>(
request: object,
endpoint: Endpoint,
key: string,
): Promise<T> {
): Promise<[T, number]> {
const source = 'getSinglePage';
try {
if (request) {
Expand All @@ -41,7 +41,9 @@ export async function getSinglePage<T extends GeneralResponseData>(
'X-API-Key': key,
},
});
return (await result.json()) as T;
const body = await result.json() as T
const remainingRateLimit = parseInt(result.headers.get('X-RateLimit-Remaining'), 10) ?? 0
return [body, remainingRateLimit]
} else {
error(`${FILE} - ${source}`);
}
Expand All @@ -56,15 +58,15 @@ export async function getAllPages<T extends GeneralResponseData>(
key: string,
): Promise<T['data']> {
try {
const firstPage = await getSinglePage(request, endpoint, key);
const [firstPage, remaining] = await getSinglePage(request, endpoint, key);
let result = null;
if (isPageRequest(request)) {
const nrOfPages = firstPage.last_page;
const nrOfPages = firstPage.last_page > remaining ? remaining : firstPage.last_page
const pages = await Promise.all(
range(2, nrOfPages).map(async (l): Promise<T> => {
const req = Object.assign({}, request) as object & { page: number };
req.page = l;
return await getSinglePage(req, endpoint, key);
return await getSinglePage(req, endpoint, key)[0];
}),
);
result = firstPage.data.concat(...pages.map((p) => p.data)) as T['data'];
Expand Down

0 comments on commit 93e1cab

Please sign in to comment.