@datocms/cda-client
Advanced tools
Comparing version 0.1.7 to 0.1.9
@@ -5,4 +5,4 @@ "use strict"; | ||
function buildRequestHeaders(options) { | ||
// Headers to instruct DatoCMS on how to process the request: | ||
var headers = { | ||
Accept: 'application/json', | ||
// API token for the project | ||
@@ -9,0 +9,0 @@ Authorization: "Bearer ".concat(options.token), |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -49,3 +60,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fetchFn, response, parsedBody, _a; | ||
var fetchFn, serializedQuery, response, parsedBody, _a, autoRetry, rateLimitReset, retryCount, waitTimeInSecs; | ||
var _b; | ||
@@ -64,3 +75,4 @@ return __generator(this, function (_c) { | ||
} | ||
return [4 /*yield*/, fetchFn('https://graphql.datocms.com/', (0, buildRequestInit_1.buildRequestInit)(query, options))]; | ||
serializedQuery = typeof query === 'string' ? query : (0, graphql_web_1.print)(query); | ||
return [4 /*yield*/, fetchFn('https://graphql.datocms.com/', (0, buildRequestInit_1.buildRequestInit)(serializedQuery, options))]; | ||
case 1: | ||
@@ -80,2 +92,14 @@ response = _c.sent(); | ||
parsedBody = _a; | ||
autoRetry = 'autoRetry' in options ? options.autoRetry : true; | ||
if (!(response.status === 429 && autoRetry)) return [3 /*break*/, 7]; | ||
rateLimitReset = response.headers.get('X-RateLimit-Reset'); | ||
retryCount = (options.retryCount || 0) + 1; | ||
waitTimeInSecs = rateLimitReset | ||
? Number.parseInt(rateLimitReset, 10) | ||
: retryCount; | ||
return [4 /*yield*/, wait(waitTimeInSecs * 1000)]; | ||
case 6: | ||
_c.sent(); | ||
return [2 /*return*/, rawExecuteQuery(serializedQuery, __assign(__assign({}, options), { retryCount: retryCount }))]; | ||
case 7: | ||
if (!response.ok || | ||
@@ -89,3 +113,3 @@ typeof parsedBody === 'string' || | ||
body: parsedBody, | ||
}, typeof query === 'string' ? query : (0, graphql_web_1.print)(query), options); | ||
}, serializedQuery, options); | ||
} | ||
@@ -113,2 +137,7 @@ return [2 /*return*/, [parsedBody.data, response]]; | ||
} | ||
function wait(time) { | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve, time); | ||
}); | ||
} | ||
//# sourceMappingURL=executeQuery.js.map |
export function buildRequestHeaders(options) { | ||
// Headers to instruct DatoCMS on how to process the request: | ||
var headers = { | ||
Accept: 'application/json', | ||
// API token for the project | ||
@@ -5,0 +5,0 @@ Authorization: "Bearer ".concat(options.token), |
@@ -39,3 +39,7 @@ import type * as GraphQLWeb from '@0no-co/graphql.web'; | ||
fetchFn?: typeof fetch; | ||
autoRetry?: boolean; | ||
}; | ||
export type RawExecuteQueryOptions<Variables = unknown> = ExecuteQueryOptions<Variables> & { | ||
retryCount?: number; | ||
}; | ||
export declare function rawExecuteQuery<Result = unknown, Variables = unknown>(query: TypedDocumentNode<Result, Variables>, options: ExecuteQueryOptions<Variables>): Promise<[Result, Response]>; | ||
@@ -42,0 +46,0 @@ export declare function rawExecuteQuery<Result = unknown, Variables = unknown>(query: GraphQLWeb.DocumentNode, options: ExecuteQueryOptions<Variables>): Promise<[Result, Response]>; |
@@ -0,1 +1,12 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -45,3 +56,3 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var fetchFn, response, parsedBody, _a; | ||
var fetchFn, serializedQuery, response, parsedBody, _a, autoRetry, rateLimitReset, retryCount, waitTimeInSecs; | ||
var _b; | ||
@@ -60,3 +71,4 @@ return __generator(this, function (_c) { | ||
} | ||
return [4 /*yield*/, fetchFn('https://graphql.datocms.com/', buildRequestInit(query, options))]; | ||
serializedQuery = typeof query === 'string' ? query : print(query); | ||
return [4 /*yield*/, fetchFn('https://graphql.datocms.com/', buildRequestInit(serializedQuery, options))]; | ||
case 1: | ||
@@ -76,2 +88,14 @@ response = _c.sent(); | ||
parsedBody = _a; | ||
autoRetry = 'autoRetry' in options ? options.autoRetry : true; | ||
if (!(response.status === 429 && autoRetry)) return [3 /*break*/, 7]; | ||
rateLimitReset = response.headers.get('X-RateLimit-Reset'); | ||
retryCount = (options.retryCount || 0) + 1; | ||
waitTimeInSecs = rateLimitReset | ||
? Number.parseInt(rateLimitReset, 10) | ||
: retryCount; | ||
return [4 /*yield*/, wait(waitTimeInSecs * 1000)]; | ||
case 6: | ||
_c.sent(); | ||
return [2 /*return*/, rawExecuteQuery(serializedQuery, __assign(__assign({}, options), { retryCount: retryCount }))]; | ||
case 7: | ||
if (!response.ok || | ||
@@ -85,3 +109,3 @@ typeof parsedBody === 'string' || | ||
body: parsedBody, | ||
}, typeof query === 'string' ? query : print(query), options); | ||
}, serializedQuery, options); | ||
} | ||
@@ -109,2 +133,7 @@ return [2 /*return*/, [parsedBody.data, response]]; | ||
} | ||
function wait(time) { | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve, time); | ||
}); | ||
} | ||
//# sourceMappingURL=executeQuery.js.map |
@@ -39,3 +39,7 @@ import type * as GraphQLWeb from '@0no-co/graphql.web'; | ||
fetchFn?: typeof fetch; | ||
autoRetry?: boolean; | ||
}; | ||
export type RawExecuteQueryOptions<Variables = unknown> = ExecuteQueryOptions<Variables> & { | ||
retryCount?: number; | ||
}; | ||
export declare function rawExecuteQuery<Result = unknown, Variables = unknown>(query: TypedDocumentNode<Result, Variables>, options: ExecuteQueryOptions<Variables>): Promise<[Result, Response]>; | ||
@@ -42,0 +46,0 @@ export declare function rawExecuteQuery<Result = unknown, Variables = unknown>(query: GraphQLWeb.DocumentNode, options: ExecuteQueryOptions<Variables>): Promise<[Result, Response]>; |
{ | ||
"name": "@datocms/cda-client", | ||
"version": "0.1.7", | ||
"version": "0.1.9", | ||
"description": "JS client for DatoCMS GraphQL Content Delivery API based on the browser Fetch API", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/index.js", |
@@ -21,4 +21,4 @@ export type BuildRequestHeadersOptions = { | ||
): HeadersInit { | ||
// Headers to instruct DatoCMS on how to process the request: | ||
const headers: HeadersInit = { | ||
Accept: 'application/json', | ||
// API token for the project | ||
@@ -25,0 +25,0 @@ Authorization: `Bearer ${options.token}`, |
@@ -47,4 +47,8 @@ import type * as GraphQLWeb from '@0no-co/graphql.web'; | ||
fetchFn?: typeof fetch; | ||
autoRetry?: boolean; | ||
}; | ||
export type RawExecuteQueryOptions<Variables = unknown> = | ||
ExecuteQueryOptions<Variables> & { retryCount?: number }; | ||
export function rawExecuteQuery<Result = unknown, Variables = unknown>( | ||
@@ -70,3 +74,3 @@ query: TypedDocumentNode<Result, Variables>, | ||
query: string | GraphQLWeb.DocumentNode, | ||
options: ExecuteQueryOptions<Variables>, | ||
options: RawExecuteQueryOptions<Variables>, | ||
) { | ||
@@ -88,5 +92,7 @@ if (!query) { | ||
const serializedQuery = typeof query === 'string' ? query : print(query); | ||
const response = await fetchFn( | ||
'https://graphql.datocms.com/', | ||
buildRequestInit(query, options), | ||
buildRequestInit(serializedQuery, options), | ||
); | ||
@@ -103,2 +109,20 @@ | ||
const autoRetry = 'autoRetry' in options ? options.autoRetry : true; | ||
if (response.status === 429 && autoRetry) { | ||
const rateLimitReset = response.headers.get('X-RateLimit-Reset'); | ||
const retryCount = (options.retryCount || 0) + 1; | ||
const waitTimeInSecs = rateLimitReset | ||
? Number.parseInt(rateLimitReset, 10) | ||
: retryCount; | ||
await wait(waitTimeInSecs * 1000); | ||
return rawExecuteQuery<Result, Variables>(serializedQuery, { | ||
...options, | ||
retryCount, | ||
} as RawExecuteQueryOptions<Variables>); | ||
} | ||
if ( | ||
@@ -116,3 +140,3 @@ !response.ok || | ||
}, | ||
typeof query === 'string' ? query : print(query), | ||
serializedQuery, | ||
options, | ||
@@ -150,1 +174,7 @@ ); | ||
} | ||
function wait(time: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, time); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
50073
881