datocms-listen
Advanced tools
Comparing version 0.1.8 to 0.1.9
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
exports.__esModule = true; | ||
__export(require("./subscribeToQuery")); | ||
__exportStar(require("./subscribeToQuery"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -38,8 +38,10 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
@@ -53,3 +55,3 @@ var __importDefault = (this && this.__importDefault) || function (mod) { | ||
var makeFakeFetch = function (_a) { | ||
var _b = (_a === void 0 ? {} : _a).serverErrors, serverErrors = _b === void 0 ? 1 : _b; | ||
var _b = _a === void 0 ? {} : _a, _c = _b.serverErrors, serverErrors = _c === void 0 ? 1 : _c; | ||
var times = 0; | ||
@@ -92,5 +94,5 @@ var fetcher = function () { return __awaiter(void 0, void 0, void 0, function () { | ||
MyEventSource.prototype.addEventListener = function (event, cb) { | ||
this.stream.listeners[event] = __spreadArrays((this.stream.listeners[event] || []), [ | ||
this.stream.listeners[event] = __spreadArray(__spreadArray([], (this.stream.listeners[event] || []), true), [ | ||
cb, | ||
]); | ||
], false); | ||
}; | ||
@@ -112,3 +114,3 @@ MyEventSource.prototype.close = function () { | ||
fetcher = makeFakeFetch(); | ||
unsubscribePromise = index_1.subscribeToQuery({ | ||
unsubscribePromise = (0, index_1.subscribeToQuery)({ | ||
query: "{ allBlogPosts(first: 1) { title } }", | ||
@@ -142,4 +144,4 @@ token: "XXX", | ||
fetcher = makeFakeFetch(); | ||
onChannelErrorDefer = p_defer_1["default"](); | ||
index_1.subscribeToQuery({ | ||
onChannelErrorDefer = (0, p_defer_1["default"])(); | ||
(0, index_1.subscribeToQuery)({ | ||
query: "{ allBlogPosts(first: 1) { title } }", | ||
@@ -186,4 +188,4 @@ token: "XXX", | ||
fetcher = makeFakeFetch(); | ||
onUpdateEventDefer = p_defer_1["default"](); | ||
index_1.subscribeToQuery({ | ||
onUpdateEventDefer = (0, p_defer_1["default"])(); | ||
(0, index_1.subscribeToQuery)({ | ||
query: "{ allBlogPosts(first: 1) { title } }", | ||
@@ -227,4 +229,4 @@ token: "XXX", | ||
fetcher = makeFakeFetch(); | ||
onEventDefer = p_defer_1["default"](); | ||
index_1.subscribeToQuery({ | ||
onEventDefer = (0, p_defer_1["default"])(); | ||
(0, index_1.subscribeToQuery)({ | ||
query: "{ allBlogPosts(first: 1) { title } }", | ||
@@ -256,4 +258,4 @@ token: "XXX", | ||
fetcher = makeFakeFetch({ serverErrors: 0 }); | ||
onErrorDefer = p_defer_1["default"](); | ||
index_1.subscribeToQuery({ | ||
onErrorDefer = (0, p_defer_1["default"])(); | ||
(0, index_1.subscribeToQuery)({ | ||
query: "{ allBlogPosts(first: 1) { title } }", | ||
@@ -260,0 +262,0 @@ token: "XXX", |
@@ -34,4 +34,11 @@ export declare type UpdateData<QueryResult> = { | ||
token: string; | ||
/** If true, the Content Delivery API with draft content will be used */ | ||
/** | ||
* If true, the Content Delivery API with draft content will be used | ||
* @deprecated use includeDrafts instead | ||
**/ | ||
preview?: boolean; | ||
/** If true, draft records will be returned */ | ||
includeDrafts?: boolean; | ||
/** If true, invalid records will be filtered out */ | ||
excludeInvalid?: boolean; | ||
/** The name of the DatoCMS environment where to perform the query (defaults to primary environment) */ | ||
@@ -38,0 +45,0 @@ environment?: string; |
@@ -6,6 +6,8 @@ "use strict"; | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
@@ -64,13 +66,3 @@ function __() { this.constructor = d; } | ||
exports.__esModule = true; | ||
function endpointFactory(_a) { | ||
var baseUrl = _a.baseUrl, preview = _a.preview, environment = _a.environment; | ||
var result = baseUrl; | ||
if (environment) { | ||
result += "/environments/" + environment; | ||
} | ||
if (preview) { | ||
result += "/preview"; | ||
} | ||
return result; | ||
} | ||
exports.subscribeToQuery = void 0; | ||
var Response500Error = /** @class */ (function (_super) { | ||
@@ -105,3 +97,3 @@ __extends(Response500Error, _super); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var query, token, variables, preview, environment, customFetcher, customEventSourceClass, onStatusChange, onUpdate, onChannelError, onError, onEvent, customReconnectionPeriod, customBaseUrl, fetcher, EventSourceClass, reconnectionPeriod, baseUrl, waitAndReconnect, channelUrl, req, registration, e_1, data, event_1; | ||
var query, token, variables, preview, includeDrafts, excludeInvalid, environment, customFetcher, customEventSourceClass, onStatusChange, onUpdate, onChannelError, onError, onEvent, customReconnectionPeriod, customBaseUrl, fetcher, EventSourceClass, reconnectionPeriod, baseUrl, waitAndReconnect, channelUrl, req, registration, e_1, data, event_1; | ||
var _this = this; | ||
@@ -111,3 +103,3 @@ return __generator(this, function (_a) { | ||
case 0: | ||
query = options.query, token = options.token, variables = options.variables, preview = options.preview, environment = options.environment, customFetcher = options.fetcher, customEventSourceClass = options.eventSourceClass, onStatusChange = options.onStatusChange, onUpdate = options.onUpdate, onChannelError = options.onChannelError, onError = options.onError, onEvent = options.onEvent, customReconnectionPeriod = options.reconnectionPeriod, customBaseUrl = options.baseUrl; | ||
query = options.query, token = options.token, variables = options.variables, preview = options.preview, includeDrafts = options.includeDrafts, excludeInvalid = options.excludeInvalid, environment = options.environment, customFetcher = options.fetcher, customEventSourceClass = options.eventSourceClass, onStatusChange = options.onStatusChange, onUpdate = options.onUpdate, onChannelError = options.onChannelError, onError = options.onError, onEvent = options.onEvent, customReconnectionPeriod = options.reconnectionPeriod, customBaseUrl = options.baseUrl; | ||
fetcher = customFetcher || window.fetch; | ||
@@ -133,7 +125,4 @@ EventSourceClass = customEventSourceClass || window.EventSource; | ||
_a.trys.push([1, 4, , 5]); | ||
return [4 /*yield*/, fetcher(endpointFactory({ baseUrl: baseUrl, preview: preview, environment: environment }), { | ||
headers: { | ||
Authorization: "Bearer " + token, | ||
Accept: "application/json" | ||
}, | ||
return [4 /*yield*/, fetcher(baseUrl, { | ||
headers: __assign(__assign(__assign({ Authorization: "Bearer ".concat(token), Accept: "application/json" }, (environment ? { 'X-Environment': environment } : {})), (includeDrafts || preview ? { 'X-Include-Drafts': 'true' } : {})), (excludeInvalid ? { 'X-Exclude-Invalid': 'true' } : {})), | ||
method: 'POST', | ||
@@ -145,9 +134,9 @@ body: JSON.stringify({ query: query, variables: variables }) | ||
if (req.status >= 300 && req.status < 500) { | ||
throw new Response400Error("Invalid status code: " + req.status + " " + req.statusText); | ||
throw new Response400Error("Invalid status code: ".concat(req.status, " ").concat(req.statusText)); | ||
} | ||
if (req.status >= 500) { | ||
throw new Response500Error("Invalid status code: " + req.status + " " + req.statusText); | ||
throw new Response500Error("Invalid status code: ".concat(req.status, " ").concat(req.statusText)); | ||
} | ||
if (req.headers.get('Content-Type') !== 'application/json') { | ||
throw new InvalidResponseError("Invalid content type: " + req.headers.get('Content-Type')); | ||
throw new InvalidResponseError("Invalid content type: ".concat(req.headers.get('Content-Type'))); | ||
} | ||
@@ -159,3 +148,7 @@ return [4 /*yield*/, req.json()]; | ||
if (onEvent) { | ||
onEvent({ status: 'connecting', channelUrl: channelUrl, message: 'Received channel URL' }); | ||
onEvent({ | ||
status: 'connecting', | ||
channelUrl: channelUrl, | ||
message: 'Received channel URL' | ||
}); | ||
} | ||
@@ -162,0 +155,0 @@ return [3 /*break*/, 5]; |
{ | ||
"name": "datocms-listen", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"types": "dist/index.d.ts", | ||
@@ -18,7 +18,7 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^25.1.4", | ||
"@types/jest": "^26.0.0", | ||
"@types/raf": "^3.4.0", | ||
"@types/rimraf": "^3.0.0", | ||
"doctoc": "^1.4.0", | ||
"jest": "^25.1.0", | ||
"jest": "^26.6.3", | ||
"np": "^6.5.0", | ||
@@ -29,3 +29,3 @@ "p-defer": "^3.0.0", | ||
"ts-jest": "^26.4.1", | ||
"typescript": "^3.8.3" | ||
"typescript": "^4.7.3" | ||
}, | ||
@@ -32,0 +32,0 @@ "scripts": { |
@@ -20,26 +20,2 @@ export type UpdateData<QueryResult> = { | ||
type EndpointFactoryOptions = { | ||
baseUrl: string; | ||
preview?: boolean; | ||
environment?: string; | ||
}; | ||
function endpointFactory({ | ||
baseUrl, | ||
preview, | ||
environment, | ||
}: EndpointFactoryOptions) { | ||
let result = baseUrl; | ||
if (environment) { | ||
result += `/environments/${environment}`; | ||
} | ||
if (preview) { | ||
result += `/preview`; | ||
} | ||
return result; | ||
} | ||
export type ConnectionStatus = 'connecting' | 'connected' | 'closed'; | ||
@@ -63,4 +39,11 @@ | ||
token: string; | ||
/** If true, the Content Delivery API with draft content will be used */ | ||
/** | ||
* If true, the Content Delivery API with draft content will be used | ||
* @deprecated use includeDrafts instead | ||
**/ | ||
preview?: boolean; | ||
/** If true, draft records will be returned */ | ||
includeDrafts?: boolean; | ||
/** If true, invalid records will be filtered out */ | ||
excludeInvalid?: boolean; | ||
/** The name of the DatoCMS environment where to perform the query (defaults to primary environment) */ | ||
@@ -129,2 +112,4 @@ environment?: string; | ||
preview, | ||
includeDrafts, | ||
excludeInvalid, | ||
environment, | ||
@@ -163,13 +148,13 @@ fetcher: customFetcher, | ||
try { | ||
const req = await fetcher( | ||
endpointFactory({ baseUrl, preview, environment }), | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
Accept: `application/json`, | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify({ query, variables }), | ||
const req = await fetcher(baseUrl, { | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
Accept: `application/json`, | ||
...(environment ? { 'X-Environment': environment } : {}), | ||
...(includeDrafts || preview ? { 'X-Include-Drafts': 'true' } : {}), | ||
...(excludeInvalid ? { 'X-Exclude-Invalid': 'true' } : {}), | ||
}, | ||
); | ||
method: 'POST', | ||
body: JSON.stringify({ query, variables }), | ||
}); | ||
@@ -198,3 +183,7 @@ if (req.status >= 300 && req.status < 500) { | ||
if (onEvent) { | ||
onEvent({status: 'connecting', channelUrl, message: 'Received channel URL'}); | ||
onEvent({ | ||
status: 'connecting', | ||
channelUrl, | ||
message: 'Received channel URL', | ||
}); | ||
} | ||
@@ -207,5 +196,5 @@ } catch (e) { | ||
if (onError) { | ||
const data = JSON.stringify({message: e.message}); | ||
const event = new MessageEvent('FetchError', {data}); | ||
onError(event) | ||
const data = JSON.stringify({ message: e.message }); | ||
const event = new MessageEvent('FetchError', { data }); | ||
onError(event); | ||
} | ||
@@ -273,3 +262,3 @@ | ||
const messageEvent = (event as MessageEvent); | ||
const messageEvent = event as MessageEvent; | ||
if (onError) { | ||
@@ -276,0 +265,0 @@ onError(messageEvent); |
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
64632
1021