From e1c09845680b0cb0a3ce41d2ebc9514ce54fdb89 Mon Sep 17 00:00:00 2001 From: Leif Shackelford Date: Tue, 5 Jul 2022 13:41:42 -0700 Subject: [PATCH] headers --- dist/fetch.d.ts | 4 +- dist/fetch.js | 36 +++++------ dist/fetch.js.map | 2 +- dist/index.js | 24 ++++---- dist/index.js.map | 2 +- package.json | 2 +- src/fetch.ts | 148 ++++++++++++++++++++++++---------------------- 7 files changed, 114 insertions(+), 104 deletions(-) diff --git a/dist/fetch.d.ts b/dist/fetch.d.ts index af02c1e..9a69d96 100644 --- a/dist/fetch.d.ts +++ b/dist/fetch.d.ts @@ -12,7 +12,9 @@ export interface FetchOptions { [x: string]: string; }; } -export declare function getPostHeaders({ baseHeaders, additionalHeaders, }: Partial): { +export declare function getHeaders({ isPost, baseHeaders, additionalHeaders, }: Partial & { + isPost: boolean; +}): { [x: string]: string; }; export declare const getOptions: (_options: FetchOptions | string) => any; diff --git a/dist/fetch.js b/dist/fetch.js index 89e7e91..97c17ce 100644 --- a/dist/fetch.js +++ b/dist/fetch.js @@ -22,24 +22,29 @@ var __rest = (this && this.__rest) || function (s, e) { return t; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.uapiFetch = exports.getOptions = exports.getPostHeaders = void 0; +exports.uapiFetch = exports.getOptions = exports.getHeaders = void 0; require("isomorphic-fetch"); // @ts-ignore if (typeof globalThis.navigator === "undefined") globalThis.navigator = {}; -function getPostHeaders(_a) { - var baseHeaders = _a.baseHeaders, additionalHeaders = _a.additionalHeaders; - var headersMap = __assign(__assign({}, (baseHeaders || { - "Accept-Encoding": "gzip", - Accept: "application/json", - "Content-Type": "application/json", - })), additionalHeaders); +function getHeaders(_a) { + var isPost = _a.isPost, baseHeaders = _a.baseHeaders, additionalHeaders = _a.additionalHeaders; + var headers = baseHeaders && Object.keys(baseHeaders).length > 0 + ? baseHeaders + : isPost + ? { + "Accept-Encoding": "gzip", + Accept: "application/json", + "Content-Type": "application/json", + } + : { "Accept-Encoding": "gzip", Accept: "application/json" }; + var headersMap = __assign(__assign({}, headers), additionalHeaders); return headersMap; } -exports.getPostHeaders = getPostHeaders; +exports.getHeaders = getHeaders; var getOptions = function (_options) { var options; - if (!_options || (typeof _options === 'string')) { + if (!_options || typeof _options === "string") { options = {}; } else { @@ -47,12 +52,9 @@ var getOptions = function (_options) { } var baseHeaders = options.baseHeaders, additionalHeaders = options.additionalHeaders, apiKey = options.apiKey, _method = options.method, _body = options.body, mode = options.mode, passThrough = __rest(options, ["baseHeaders", "additionalHeaders", "apiKey", "method", "body", "mode"]); var method = _method || "GET"; - var isPost = ['put', 'post'].indexOf(method.toLowerCase()) !== -1; + var isPost = ["put", "post"].indexOf(method.toLowerCase()) !== -1; var headers = new Headers(); - var headersMap = isPost ? getPostHeaders({ baseHeaders: baseHeaders, additionalHeaders: additionalHeaders }) : __assign(__assign({}, (baseHeaders || { - "Accept-Encoding": "gzip", - "Accept": "*/*", - })), additionalHeaders); + var headersMap = getHeaders({ baseHeaders: baseHeaders, additionalHeaders: additionalHeaders, isPost: isPost }); for (var _i = 0, _a = Object.keys(headersMap); _i < _a.length; _i++) { var k = _a[_i]; headers.append(k, headersMap[k]); @@ -67,7 +69,7 @@ var getOptions = function (_options) { } var body; if (isPost) { - if (typeof _body == 'string') { + if (typeof _body == "string") { body = _body; } else if (_body) { @@ -78,7 +80,7 @@ var getOptions = function (_options) { }; exports.getOptions = getOptions; var uapiFetch = function (_options) { - var _a = exports.getOptions(_options), uri = _a.uri, iFetchOptions = __rest(_a, ["uri"]); + var _a = (0, exports.getOptions)(_options), uri = _a.uri, iFetchOptions = __rest(_a, ["uri"]); // console.log(iFetchOptions); return fetch(uri, iFetchOptions); }; diff --git a/dist/fetch.js.map b/dist/fetch.js.map index 32e087b..e61e834 100644 --- a/dist/fetch.js.map +++ b/dist/fetch.js.map @@ -1 +1 @@ -{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../src/fetch.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;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AAyB,IAChC;AACJ,CAAC,CAAC;AAnDW,QAAA,UAAU,cAmDrB;AAEK,IAAM,SAAS,GAAG,UAAC,QAA+B;IACvD,IAAM,KAA4B,IAAA,kBAAU,EAAC,QAAQ,CAAC,EAA9C,GAAG,SAAA,EAAK,aAAa,cAAvB,OAAyB,CAAuB,CAAC;IACvD,8BAA8B;IAC9B,OAAO,KAAK,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;AACnC,CAAC,CAAC;AAJW,QAAA,SAAS,aAIpB"} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 190a53e..67822e0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -73,18 +73,18 @@ var getQr = function (qr, route, options) { var _a, endpoint, pathComponents, vars, qrPathDefinition, uri, endpointPathDefinition, res; return __generator(this, function (_b) { switch (_b.label) { - case 0: return [4 /*yield*/, exports.fetchEndpointInfo(qr)]; + case 0: return [4 /*yield*/, (0, exports.fetchEndpointInfo)(qr)]; case 1: _a = _b.sent(), endpoint = _a.endpoint, pathComponents = _a["path-components"]; vars = {}; qrPathDefinition = pathComponents; uri = endpoint; if (qrPathDefinition) { - vars = lib_1.parsePathComponents(qr, qrPathDefinition); - endpointPathDefinition = endpoint + "/" + exports.Endpoints[route].get; - uri = lib_1.applyPathComponents(endpointPathDefinition, vars); + vars = (0, lib_1.parsePathComponents)(qr, qrPathDefinition); + endpointPathDefinition = "".concat(endpoint, "/").concat(exports.Endpoints[route].get); + uri = (0, lib_1.applyPathComponents)(endpointPathDefinition, vars); } - return [4 /*yield*/, fetch_1.uapiFetch(__assign({ uri: uri }, options))]; + return [4 /*yield*/, (0, fetch_1.uapiFetch)(__assign({ uri: uri }, options))]; case 2: res = _b.sent(); return [4 /*yield*/, res.json()]; @@ -105,19 +105,19 @@ var postQr = function (qr, route, options) { switch (_a.label) { case 0: url = new URL(qr); - return [4 /*yield*/, exports.fetchEndpointInfo(qr)]; + return [4 /*yield*/, (0, exports.fetchEndpointInfo)(qr)]; case 1: epInfo = _a.sent(); vars = {}; qrPathDefinition = epInfo['path-components']; uri = epInfo.endpoint; if (qrPathDefinition) { - vars = lib_1.parsePathComponents(qr, qrPathDefinition); + vars = (0, lib_1.parsePathComponents)(qr, qrPathDefinition); console.log("apply path vars", vars); - endpointPathDefinition = epInfo.endpoint + "/" + exports.Endpoints[route].get; - uri = lib_1.applyPathComponents(endpointPathDefinition, vars); + endpointPathDefinition = "".concat(epInfo.endpoint, "/").concat(exports.Endpoints[route].get); + uri = (0, lib_1.applyPathComponents)(endpointPathDefinition, vars); } - return [4 /*yield*/, fetch_1.uapiFetch(__assign({ uri: uri }, options))]; + return [4 /*yield*/, (0, fetch_1.uapiFetch)(__assign({ uri: uri }, options))]; case 2: res = _a.sent(); return [4 /*yield*/, res.json()]; @@ -136,8 +136,8 @@ var fetchEndpointInfo = function (qr, apiKey) { return __awaiter(void 0, void 0, url = new URL(qr); if (info[url.host] !== undefined) return [2 /*return*/, info[url.host]]; - endpoint = "https://" + url.host; - endpointInfo = endpoint + "/.well-known/cannabis-api.json"; + endpoint = "https://".concat(url.host); + endpointInfo = "".concat(endpoint, "/.well-known/cannabis-api.json"); return [4 /*yield*/, fetch(endpointInfo)]; case 1: res = _a.sent(); diff --git a/dist/index.js.map b/dist/index.js.map index a0e9440..e1e6907 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,6BAAiE;AACjE,iCAA8D;AAoErD,0FApEA,iBAAS,OAoEA;AACT,2FArEyB,kBAAU,OAqEzB;AAzDnB,IAAM,IAAI,GAA+B,EAAE,CAAC;AAE/B,QAAA,SAAS,GAAG;IACrB,IAAI,EAAE;QACF,GAAG,EAAE,YAAY;QACjB,IAAI,EAAE,YAAY;KACrB;IACD,IAAI,EAAE;QACF,GAAG,EAAE,YAAY;QACjB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G,UAAO,EAAU,EAAE,MAAe;;;;;gBACzD,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC,CAAC;gBACxB,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS;oBAAE,sBAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAC;gBAElD,QAAQ,GAAG,kBAAW,GAAG,CAAC,IAAI,CAAE,CAAC;gBACjC,YAAY,GAAG,UAAG,QAAQ,mCAAgC,CAAC;gBACrD,qBAAM,KAAK,CAAC,YAAY,CAAC,EAAA;;gBAA/B,GAAG,GAAG,SAAyB;gBACxB,qBAAM,GAAG,CAAC,IAAI,EAAE,EAAA;;gBAAvB,IAAI,GAAG,SAAgB;gBAE7B,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;gBACtB,sBAAO,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAC;;;KACzB,CAAA;AAXY,QAAA,iBAAiB,qBAW7B"} \ No newline at end of file diff --git a/package.json b/package.json index 6cef749..207cc1c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "@cannabis-labeling-api/fetch", "private": false, "description": "fetch QR details from a compliant Cannabis Labeling endpoint", - "version": "0.1.3", + "version": "0.1.4", "homepage": "https://github.com/cannabis-labeling-api/fetch", "author": "Leif Shackelford ", "engines": { diff --git a/src/fetch.ts b/src/fetch.ts index 3251574..89bfef3 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -1,90 +1,96 @@ import "isomorphic-fetch"; export interface FetchOptions { - uri?: string - body?: any; - mode?: string; - method?: string; - apiKey?: string - baseHeaders?: { [x: string]: string }; - additionalHeaders?: { [x: string]: string }; + uri?: string; + body?: any; + mode?: string; + method?: string; + apiKey?: string; + baseHeaders?: { [x: string]: string }; + additionalHeaders?: { [x: string]: string }; } // @ts-ignore if (typeof globalThis.navigator === "undefined") globalThis.navigator = {}; +export function getHeaders({ + isPost, + baseHeaders, + additionalHeaders, +}: Partial & { isPost: boolean }) { + const headers = + baseHeaders && Object.keys(baseHeaders).length > 0 + ? baseHeaders + : isPost + ? { + "Accept-Encoding": "gzip", + Accept: "application/json", + "Content-Type": "application/json", + } + : { "Accept-Encoding": "gzip", Accept: "application/json" }; + const headersMap = { + ...headers, + ...additionalHeaders, + }; -export function getPostHeaders({ - baseHeaders, - additionalHeaders, -}: Partial) { - const headersMap = { - ...(baseHeaders || { - "Accept-Encoding": "gzip", - Accept: "application/json", - "Content-Type": "application/json", - }), - ...additionalHeaders, - }; - - return headersMap; + return headersMap; } - export const getOptions = (_options: FetchOptions | string) => { - let options; - if (!_options || (typeof _options === 'string')) { - options = {}; - } else { - options = _options; - } - const { baseHeaders, additionalHeaders, apiKey, method: _method, body: _body, mode, ...passThrough } = options; - const method = _method || "GET"; - const isPost = ['put', 'post'].indexOf(method.toLowerCase()) !== -1; - const headers = new Headers(); - + let options; + if (!_options || typeof _options === "string") { + options = {}; + } else { + options = _options; + } + const { + baseHeaders, + additionalHeaders, + apiKey, + method: _method, + body: _body, + mode, + ...passThrough + } = options; + const method = _method || "GET"; + const isPost = ["put", "post"].indexOf(method.toLowerCase()) !== -1; + const headers = new Headers(); - const headersMap = isPost ? getPostHeaders({ baseHeaders, additionalHeaders }) : { - ...(baseHeaders || { - "Accept-Encoding": "gzip", - "Accept": "*/*", - }), - ...additionalHeaders, - }; - for (const k of Object.keys(headersMap)) { - headers.append(k, headersMap[k]); - } + const headersMap = getHeaders({ baseHeaders, additionalHeaders, isPost }); + for (const k of Object.keys(headersMap)) { + headers.append(k, headersMap[k]); + } - if (navigator && navigator.userAgent) { - headers.append("User-Agent", navigator.userAgent); - } + if (navigator && navigator.userAgent) { + headers.append("User-Agent", navigator.userAgent); + } - if (apiKey) { - const encoded = "Bearer " + apiKey; - // Send both of these!! - headers.append("Authorization", encoded); - } + if (apiKey) { + const encoded = "Bearer " + apiKey; + // Send both of these!! + headers.append("Authorization", encoded); + } - let body; - if (isPost) { - if (typeof _body == 'string') { - body = _body; - } else if (_body) { - body = JSON.stringify(_body); - } + let body; + if (isPost) { + if (typeof _body == "string") { + body = _body; + } else if (_body) { + body = JSON.stringify(_body); } - return { - ...passThrough, - method, - body, - headers, - mode: mode !== undefined ? mode : "cors", - cache: "default" as RequestCache, - }; -} + } + return { + ...passThrough, + method, + body, + headers, + mode: mode !== undefined ? mode : "cors", + cache: "default" as RequestCache, + }; +}; export const uapiFetch = (_options: FetchOptions | string) => { - const { uri, ...iFetchOptions } = getOptions(_options); - // console.log(iFetchOptions); - return fetch(uri, iFetchOptions); -} \ No newline at end of file + const { uri, ...iFetchOptions } = getOptions(_options); + // console.log(iFetchOptions); + return fetch(uri, iFetchOptions); +};