diff --git a/.gitignore b/.gitignore index 0ae7e5c..7bd780d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules /lib + +.env \ No newline at end of file diff --git a/examples.http b/examples.http new file mode 100644 index 0000000..7561fd5 --- /dev/null +++ b/examples.http @@ -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}} + + + diff --git a/package-lock.json b/package-lock.json index 4202d0f..0df6a85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1979,6 +1979,11 @@ } } }, + "data-uri-to-buffer": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-3.0.1.tgz", + "integrity": "sha512-WboRycPNsVw3B3TL559F7kuBUM4d8CgMEvk6xEJlOp7OBPjt6G7z8WMWlD2rOFZLk6OYfFIUGsCOWzcQH9K2og==" + }, "data-urls": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/data-urls/-/data-urls-2.0.0.tgz", @@ -2200,6 +2205,14 @@ "bser": "2.1.1" } }, + "fetch-blob": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.1.3.tgz", + "integrity": "sha512-ax1Y5I9w+9+JiM+wdHkhBoxew+zG4AJ2SvAD1v1szpddUIiPERVGBxrMcB2ZqW0Y3PP8bOWYv2zqQq1Jp2kqUQ==", + "requires": { + "web-streams-polyfill": "^3.0.3" + } + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -3264,32 +3277,12 @@ "dev": true }, "node-fetch": { - "version": "2.6.5", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.5.tgz", - "integrity": "sha512-mmlIVHJEu5rnIxgEgez6b9GgWXbkZj5YZ7fx+2r94a2E+Uirsp6HsPTPlomfdHtpt/B0cdKviwkoaM6pyvUOpQ==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.0.0.tgz", + "integrity": "sha512-bKMI+C7/T/SPU1lKnbQbwxptpCrG9ashG+VkytmXCPZyuM9jB6VU+hY0oi4lC8LxTtAeWdckNCTa3nrGsAdA3Q==", "requires": { - "whatwg-url": "^5.0.0" - }, - "dependencies": { - "tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" - }, - "webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" - }, - "whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", - "requires": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - } + "data-uri-to-buffer": "^3.0.1", + "fetch-blob": "^3.1.2" } }, "node-int64": { @@ -4131,6 +4124,11 @@ "makeerror": "1.0.x" } }, + "web-streams-polyfill": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.1.1.tgz", + "integrity": "sha512-Czi3fG883e96T4DLEPRvufrF2ydhOOW1+1a6c3gNjH2aIh50DNFBdfwh2AKoOf1rXvpvavAoA11Qdq9+BKjE0Q==" + }, "webidl-conversions": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-6.1.0.tgz", diff --git a/package.json b/package.json index 22b05eb..b62e652 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "typescript-tslint-plugin": "^1.0.1" }, "dependencies": { - "node-fetch": "^2.6.0" + "node-fetch": "^3.0.0" }, "files": [ "lib/**/*" diff --git a/src/api.ts b/src/api.ts index 46988e0..07c0c12 100644 --- a/src/api.ts +++ b/src/api.ts @@ -6,10 +6,10 @@ 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 { - return getSinglePage(request, Endpoint.TRANSACTIONS, this.key); + return getSinglePage(request, Endpoint.TRANSACTIONS, this.key)[0]; } async transactionsTotal(request: TransactionRequest): Promise { @@ -17,7 +17,7 @@ export class WeCanTrack { } async clickoutsPage(request: ClickoutRequest): Promise { - return getSinglePage(request, Endpoint.CLICKOUTS, this.key); + return getSinglePage(request, Endpoint.CLICKOUTS, this.key)[0]; } async clickoutsTotal(request: ClickoutRequest): Promise { diff --git a/src/types.ts b/src/types.ts index a0890f5..671d313 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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'; diff --git a/src/utils.ts b/src/utils.ts index f4b6241..9d913ae 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,7 +30,7 @@ export async function getSinglePage( request: object, endpoint: Endpoint, key: string, -): Promise { +): Promise<[T, number]> { const source = 'getSinglePage'; try { if (request) { @@ -41,7 +41,9 @@ export async function getSinglePage( '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}`); } @@ -56,15 +58,15 @@ export async function getAllPages( key: string, ): Promise { 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 => { 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'];