diff --git a/dist/index.d.ts b/dist/index.d.ts index 9c0ae62..7c6b1c4 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2,7 +2,7 @@ import { uapiFetch, FetchOptions, getOptions } from "./fetch"; export { FetchOptions }; export interface WellKnown { endpoint: string; - 'path-components'?: string; + "path-components"?: string; vendor?: string; example?: { qr: string; @@ -23,7 +23,7 @@ export declare const Endpoints: { }; }; export declare const getQr: (qr: string, route?: string, options?: FetchOptions) => Promise; -export declare const postQr: (qr: string, route?: string, options?: FetchOptions) => Promise; +export declare const postQr: (qr: string, route?: string, options?: FetchOptions) => Promise; export { postQr as putQr }; export { uapiFetch }; export { getOptions }; diff --git a/dist/index.js b/dist/index.js index 67822e0..3a52bfa 100644 --- a/dist/index.js +++ b/dist/index.js @@ -56,7 +56,7 @@ var info = {}; exports.Endpoints = { case: { get: "case/{:id}", - post: "case/{:id}" + post: "case/{:id}", }, each: { get: "each/{:id}", @@ -64,8 +64,8 @@ exports.Endpoints = { }, regulator: { get: "regulator/{:id}", - post: "regulator/{:id}" - } + post: "regulator/{:id}", + }, }; var getQr = function (qr, route, options) { if (route === void 0) { route = "case"; } @@ -96,35 +96,29 @@ var getQr = function (qr, route, options) { exports.getQr = getQr; var postQr = function (qr, route, options) { if (route === void 0) { route = "case"; } - // if (!apiKey) { - // throw new Error("please provide a JWT string to be encoded as a header"); - // } - return new Promise(function (resolve, reject) { return __awaiter(void 0, void 0, void 0, function () { - var url, epInfo, vars, qrPathDefinition, uri, endpointPathDefinition, res; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - url = new URL(qr); - return [4 /*yield*/, (0, exports.fetchEndpointInfo)(qr)]; + return __awaiter(void 0, void 0, void 0, function () { + var _a, endpoint, pathComponents, vars, qrPathDefinition, uri, endpointPathDefinition, res; + return __generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, (0, exports.fetchEndpointInfo)(qr)]; case 1: - epInfo = _a.sent(); + _a = _b.sent(), endpoint = _a.endpoint, pathComponents = _a["path-components"]; vars = {}; - qrPathDefinition = epInfo['path-components']; - uri = epInfo.endpoint; + qrPathDefinition = pathComponents; + uri = endpoint; if (qrPathDefinition) { vars = (0, lib_1.parsePathComponents)(qr, qrPathDefinition); - console.log("apply path vars", vars); - endpointPathDefinition = "".concat(epInfo.endpoint, "/").concat(exports.Endpoints[route].get); + endpointPathDefinition = "".concat(endpoint, "/").concat(exports.Endpoints[route].get); uri = (0, lib_1.applyPathComponents)(endpointPathDefinition, vars); } - return [4 /*yield*/, (0, fetch_1.uapiFetch)(__assign({ uri: uri }, options))]; + return [4 /*yield*/, (0, fetch_1.uapiFetch)(__assign({ uri: uri, method: "POST" }, options))]; case 2: - res = _a.sent(); + res = _b.sent(); return [4 /*yield*/, res.json()]; - case 3: return [2 /*return*/, _a.sent()]; + case 3: return [2 /*return*/, _b.sent()]; } }); - }); }); + }); }; exports.postQr = postQr; exports.putQr = exports.postQr; diff --git a/dist/index.js.map b/dist/index.js.map index e1e6907..d14f19d 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,IAAI,EAAE,YAAY;KACrB;IACD,SAAS,EAAE;QACP,GAAG,EAAE,iBAAiB;QACtB,IAAI,EAAE,iBAAiB;KAC1B;CACJ,CAAA;AAEM,IAAM,KAAK,GAAG,UAAO,EAAU,EAAE,KAAc,EAAE,OAAsB;IA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AK,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;;;KACvB,CAAC;AAdW,QAAA,iBAAiB,qBAc5B"} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 445ac29..4daeefb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,84 +1,88 @@ import { parsePathComponents, applyPathComponents } from "./lib"; import { uapiFetch, FetchOptions, getOptions } from "./fetch"; -export { FetchOptions } +export { FetchOptions }; export interface WellKnown { - endpoint: string // 'https://signet.codes/uapi/v1', - 'path-components'?: string // 'https://s.chroma.io/:id', - vendor?: string // 'Chroma' - example?: { - qr: string - } + endpoint: string; // 'https://signet.codes/uapi/v1', + "path-components"?: string; // 'https://s.chroma.io/:id', + vendor?: string; // 'Chroma' + example?: { + qr: string; + }; } const info: { [x: string]: WellKnown } = {}; export const Endpoints = { - case: { - get: "case/{:id}", - post: "case/{:id}" - }, - each: { - get: "each/{:id}", - post: "each/{:id}", - }, - regulator: { - get: "regulator/{:id}", - post: "regulator/{:id}" - } -} - -export const getQr = async (qr: string, route = "case", options?: FetchOptions) => { - const { endpoint, ["path-components"]: pathComponents } = await fetchEndpointInfo(qr); - let vars = {}; - const qrPathDefinition = pathComponents; - let uri = endpoint; - if (qrPathDefinition) { - vars = parsePathComponents(qr, qrPathDefinition); - const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; - uri = applyPathComponents(endpointPathDefinition, vars); - } - const res = await uapiFetch({ uri, ...options }); - return await res.json(); + case: { + get: "case/{:id}", + post: "case/{:id}", + }, + each: { + get: "each/{:id}", + post: "each/{:id}", + }, + regulator: { + get: "regulator/{:id}", + post: "regulator/{:id}", + }, +}; -} +export const getQr = async ( + qr: string, + route = "case", + options?: FetchOptions +) => { + const { endpoint, ["path-components"]: pathComponents } = + await fetchEndpointInfo(qr); + let vars = {}; + const qrPathDefinition = pathComponents; + let uri = endpoint; + if (qrPathDefinition) { + vars = parsePathComponents(qr, qrPathDefinition); + const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; + uri = applyPathComponents(endpointPathDefinition, vars); + } + const res = await uapiFetch({ uri, ...options }); + return await res.json(); +}; -export const postQr = (qr: string, route = "case", options?: FetchOptions) => { - // if (!apiKey) { - // throw new Error("please provide a JWT string to be encoded as a header"); - // } - return new Promise(async (resolve, reject) => { - const url = new URL(qr); - const epInfo = await fetchEndpointInfo(qr); - - let vars = {}; - const qrPathDefinition = epInfo['path-components']; - let uri = epInfo.endpoint; - if (qrPathDefinition) { - vars = parsePathComponents(qr, qrPathDefinition); - console.log("apply path vars", vars) - const endpointPathDefinition = `${epInfo.endpoint}/${Endpoints[route].get}`; - uri = applyPathComponents(endpointPathDefinition, vars); - } - const res = await uapiFetch({ uri, ...options }); - return await res.json(); - }) -} +export const postQr = async ( + qr: string, + route = "case", + options?: FetchOptions +) => { + const { endpoint, ["path-components"]: pathComponents } = + await fetchEndpointInfo(qr); + let vars = {}; + const qrPathDefinition = pathComponents; + let uri = endpoint; + if (qrPathDefinition) { + vars = parsePathComponents(qr, qrPathDefinition); + const endpointPathDefinition = `${endpoint}/${Endpoints[route].get}`; + uri = applyPathComponents(endpointPathDefinition, vars); + } + const res = await uapiFetch({ uri, method: "POST", ...options }); + return await res.json(); +}; export { postQr as putQr }; export { uapiFetch }; export { getOptions }; -export const fetchEndpointInfo = async (qr: string, apiKey?: string): Promise => { - const url = new URL(qr); - if (info[url.host] !== undefined) return info[url.host]; +export const fetchEndpointInfo = async ( + qr: string, + apiKey?: string +): Promise => { + const url = new URL(qr); + if (info[url.host] !== undefined) return info[url.host]; - const endpoint = `https://${url.host}`; - const endpointInfo = `${endpoint}/.well-known/cannabis-api.json`; - const res = await fetch(endpointInfo); - const body = await res.json(); + const endpoint = `https://${url.host}`; + const endpointInfo = `${endpoint}/.well-known/cannabis-api.json`; + const res = await fetch(endpointInfo); + const body = await res.json(); - info[url.host] = body; - return info[url.host]; -} \ No newline at end of file + info[url.host] = body; + return info[url.host]; +}; diff --git a/test/endpoints.test.ts b/test/endpoints.test.ts index 0903611..3f14a32 100644 --- a/test/endpoints.test.ts +++ b/test/endpoints.test.ts @@ -1,123 +1,169 @@ -import { getQr, putQr, uapiFetch, getOptions, fetchEndpointInfo } from '../dist'; +import { + getQr, + putQr, + uapiFetch, + getOptions, + fetchEndpointInfo, +} from "../dist"; -import { assert } from 'chai'; +import { assert } from "chai"; const badApiKey = "123asd"; -const env = require("./example.env.json"); +const env = require("./local.env.json"); + +const batchTag = "1A4901"; +const childTag = "1A4903"; const testFixtures = { - id: "1A4000000", - strain: "Api Kush", - category: "Api Vape 1g", - quantity: 6, - caseQuantity: 2 -} + id: batchTag, + strain: "Api Kush", + category: "Api Vape 1g", + quantity: 6, + caseQuantity: 2, +}; const endpointInfo = { - qr: env.qrBase + '/test_id' -} + qr: env.qrBase + "/test_id", +}; export const sleep = (time: number) => { - return new Promise((resolve) => setTimeout(resolve, time < 60 ? time * 1000 : time)); -} - -describe('Environment', async () => { - it(`endpoint info for ${env.qrBase}`, async () => { - const epInfo = await fetchEndpointInfo(endpointInfo.qr); - assert.exists(epInfo, 'bad endpoint info'); - // console.log(epInfo['path-components']); - // console.log(epInfo.endpoint); - endpointInfo.qr = epInfo.example.qr; - assert.notEqual(epInfo['path-components'].indexOf(env.qrBase), -1); - assert.equal(epInfo.endpoint, env.endpoint) - }); - - it('fails get with missing key', async () => { - const res = await getQr(endpointInfo.qr); - assert.equal(401, res.status); - }); - - it('fails get with bad key', async () => { - const res = await getQr(endpointInfo.qr, "case", { apiKey: badApiKey }); - assert.equal(401, res.status); - }); + return new Promise((resolve) => + setTimeout(resolve, time < 60 ? time * 1000 : time) + ); +}; + +describe("Environment", async () => { + it(`endpoint info for ${env.qrBase}`, async () => { + const epInfo = await fetchEndpointInfo(endpointInfo.qr); + assert.exists(epInfo, "bad endpoint info"); + // console.log(epInfo['path-components']); + // console.log(epInfo.endpoint); + endpointInfo.qr = epInfo.example.qr; + assert.notEqual(epInfo["path-components"].indexOf(env.qrBase), -1); + assert.equal(epInfo.endpoint, env.endpoint); + }); + + it("fails get with missing key", async () => { + const res = await getQr(endpointInfo.qr); + assert.equal(401, res.status); + }); + + it("fails get with bad key", async () => { + const res = await getQr(endpointInfo.qr, "case", { apiKey: badApiKey }); + assert.equal(401, res.status); + }); }); -describe('Test', async () => { - const fixtures = { cases: [], eaches: [], results: { added: [], removed: [] } } - - before(async () => { - const epInfo = await fetchEndpointInfo(endpointInfo.qr); - assert.exists(epInfo, 'bad endpoint info'); - endpointInfo.qr = epInfo.example.qr; - assert.notEqual(epInfo['path-components'].indexOf(env.qrBase), -1); - assert.equal(epInfo.endpoint, env.endpoint) +describe("Test", async () => { + const fixtures = { + cases: [], + eaches: [], + results: { added: [], removed: [] }, + }; + + before(async () => { + const epInfo = await fetchEndpointInfo(endpointInfo.qr); + assert.exists(epInfo, "bad endpoint info"); + endpointInfo.qr = epInfo.example.qr; + assert.notEqual(epInfo["path-components"].indexOf(env.qrBase), -1); + assert.equal(epInfo.endpoint, env.endpoint); + }); + + it("creates fixtures", async () => { + const res = await uapiFetch({ + apiKey: env.apiKey, + uri: `${env.endpoint}/fixtures/create`, + method: "put", + body: { ...testFixtures }, }); - - it('creates fixtures', async () => { - const res = await uapiFetch({ - apiKey: env.apiKey, - uri: `${env.endpoint}/fixtures/create`, - method: 'put', - body: { ...testFixtures } - }); - const body = await res.json(); - const { added } = body; - // assert.exists(added, 'add fixtures expected fixtures'); - fixtures.results.added = added; - // console.log("created fixures", added, "wait to settle"); - await sleep(1500); + const body = await res.json(); + const { added } = body; + assert.exists(added, "add fixtures expected fixtures"); + fixtures.results.added = added; + // console.log("created fixures", added, "wait to settle"); + await sleep(1500); + }); + + it(`gets cases and eaches via fixture ID: ${testFixtures.id}`, async () => { + await sleep(1500); + const res = await uapiFetch({ + apiKey: env.apiKey, + uri: `${env.endpoint}/regulator/${testFixtures.id}`, }); - - it(`gets cases and eaches via fixture ID: ${testFixtures.id}`, async () => { - await sleep(1500); - const res = await uapiFetch({ - apiKey: env.apiKey, - uri: `${env.endpoint}/regulator/${testFixtures.id}`, - }); - const { cases, eaches } = await res.json(); - // console.log({ cases, eaches }) - assert.ok(cases?.length, "no cases"); - assert.ok(eaches?.length, "no eaches"); - fixtures.cases = cases; - fixtures.eaches = eaches; + const regulatorResult = await res.json(); + // console.log(regulatorResult); + const { cases, eaches } = regulatorResult; + assert.ok(cases?.length, "no cases"); + assert.ok(eaches?.length, "no eaches"); + fixtures.cases = cases; + fixtures.eaches = eaches; + }); + + it("gets eaches from case endpoint", async () => { + const [case0] = fixtures.cases; + // console.log(case0); + const res = await getQr(case0, "case", { apiKey: env.apiKey }); + // console.log(res); + const { eaches } = await res; + assert.exists(eaches.length); + // console.log("in eaches", eaches); + for (const e of eaches) { + // console.log("expect", e); + assert.notEqual(fixtures.eaches.indexOf(e), -1); + } + }); + + it("case data from each endpoint", async () => { + const [each0] = fixtures.eaches; + // console.log("get", each0); + const res = await getQr(each0, "each", { apiKey: env.apiKey }); + const { caseId } = await res; + assert.exists(caseId); + }); + + it("appends a regulator id to a case", async () => { + const [case0] = fixtures.cases; + // console.log("get", each0); + const res = await putQr(case0, "case", { + apiKey: env.apiKey, + body: { + id: childTag, + }, }); - - it('gets eaches from case endpoint', async () => { - const [case0] = fixtures.cases; - // console.log(case0); - const res = await getQr(case0, "case", { apiKey: env.apiKey }); - // console.log(res); - const { eaches } = await res; - assert.exists(eaches.length); - // console.log("in eaches", eaches); - for (const e of eaches) { - // console.log("expect", e); - assert.notEqual(fixtures.eaches.indexOf(e), -1); - } + const { regulatorEvents } = await res; + + // console.log(regulatorEvents); + assert.exists(regulatorEvents); + assert.equal(regulatorEvents[0].id, batchTag); + assert.equal(regulatorEvents[1].id, childTag); + }); + + it("queries case/eaches by newly appended regulator id", async () => { + const res = await uapiFetch({ + apiKey: env.apiKey, + uri: `${env.endpoint}/regulator/${childTag}`, }); - - it('case data from each endpoint', async () => { - const [each0] = fixtures.eaches; - // console.log("get", each0); - const res = await getQr(each0, "each", { apiKey: env.apiKey }); - const { caseId } = await res; - assert.exists(caseId); - }); - - after(async () => { - const res = await uapiFetch({ - apiKey: env.apiKey, - method: 'put', - uri: `${env.endpoint}/fixtures/clean`, - body: { ...testFixtures } - }); - const body = await res.json(); - const { removed } = body; - fixtures.results.removed = removed; - assert.ok(removed?.branches?.length); - assert.ok(removed?.softCodes?.length); + const regulatorResult = await res.json(); + // console.log(regulatorResult); + const { cases, eaches } = regulatorResult; + assert.ok(cases?.length, "no cases"); + assert.ok(eaches?.length, "no eaches"); + fixtures.cases = cases; + fixtures.eaches = eaches; + }); + + after(async () => { + const res = await uapiFetch({ + apiKey: env.apiKey, + method: "put", + uri: `${env.endpoint}/fixtures/clean`, + body: { ...testFixtures }, }); + const body = await res.json(); + const { removed } = body; + fixtures.results.removed = removed; + assert.ok(removed?.branches?.length); + assert.ok(removed?.softCodes?.length); + // console.log("cleaned up", fixtures.results.removed); + }); }); - -