Skip to content

Commit

Permalink
Refactor entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-ignatov committed Jan 9, 2020
1 parent d84ce99 commit bddbbd6
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 101 deletions.
36 changes: 17 additions & 19 deletions dist/lib/entry/browser.d.ts
@@ -1,21 +1,19 @@

import { fhirclient } from "../types";
import Client from "../Client";

export = smart;

type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;


// tslint:disable-next-line: no-namespace
declare namespace smart {
export const oauth2: OAuth2;
export function client(stateOrURI: fhirclient.ClientState | string): Client;
}

interface OAuth2 {
settings: fhirclient.fhirSettings;
ready: fhirclient.readyFunction;
authorize: (p: fhirclient.AuthorizeParams) => Promise<never>;
init: (p: fhirclient.AuthorizeParams) => Promise<never|Client>;
}
declare const FHIR: {
AbortController: {
new (): AbortController;
prototype: AbortController;
};
client: (state: string | fhirclient.ClientState) => Client;
oauth2: {
settings: fhirclient.fhirSettings;
ready: {
(onSuccess: (client: Client) => any, onError?: (error: Error) => any): Promise<any>;
(): Promise<Client>;
};
authorize: (options: fhirclient.AuthorizeParams) => Promise<string | void>;
init: (options: fhirclient.AuthorizeParams) => Promise<Client>;
};
};
export = FHIR;
25 changes: 16 additions & 9 deletions dist/lib/entry/browser.js
@@ -1,34 +1,41 @@
"use strict";
/* eslint-env browser */
// In Browsers we create an adapter, get the SMART api from it and build the
// global FHIR object
const BrowserAdapter = require("../adapters/BrowserAdapter").default;

const BrowserAdapter_1 = require("../adapters/BrowserAdapter");

const adapter = new BrowserAdapter_1.default();
const {
ready,
authorize,
init,
client,
options
} = BrowserAdapter.smart();

// We have two kinds of browser builds - "pure" for new browsers and "legacy"
} = adapter.getSmartApi(); // We have two kinds of browser builds - "pure" for new browsers and "legacy"
// for old ones. In pure builds we assume that the browser supports everything
// we need. In legacy mode, the library also acts as a polyfill. Babel will
// automatically polyfill everything except "fetch", which we have to handle
// manually here.
// manually.
// @ts-ignore
// eslint-disable-next-line no-undef

if (typeof FHIRCLIENT_PURE == "undefined") {
const fetch = require("cross-fetch");

require("abortcontroller-polyfill/dist/abortcontroller-polyfill-only");

if (!window.fetch) {
window.fetch = fetch.default;
window.Headers = fetch.Headers;
window.Request = fetch.Request;
window.Response = fetch.Response;
}
}
} // $lab:coverage:off


// $lab:coverage:off$
module.exports = {
const FHIR = {
AbortController: window.AbortController,
client,
oauth2: {
settings: options,
Expand All @@ -37,4 +44,4 @@ module.exports = {
init
}
};
// $lab:coverage:on$
module.exports = FHIR; // $lab:coverage:on$
28 changes: 9 additions & 19 deletions dist/lib/entry/hapi.d.ts
@@ -1,21 +1,11 @@
import { fhirclient } from "../types";
import Client from "../Client";
import { Request, ResponseToolkit } from "hapi";

export = smart;

type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;

declare function smart(
req: Request,
h: ResponseToolkit,
storage?: fhirclient.Storage | storageFactory
): OAuth2;

interface OAuth2 {
settings: fhirclient.fhirSettings;
ready: fhirclient.readyFunction;
authorize: (p: fhirclient.AuthorizeParams) => Promise<never>;
init: (p: fhirclient.AuthorizeParams) => Promise<never|Client>;
import { ResponseToolkit, Request } from "hapi";
declare type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;
declare function smart(request: Request, h: ResponseToolkit, storage?: fhirclient.Storage | storageFactory): fhirclient.SMART;
declare namespace smart {
var AbortController: {
new (): AbortController;
prototype: AbortController;
};
}

export = smart;
18 changes: 16 additions & 2 deletions dist/lib/entry/hapi.js
@@ -1,2 +1,16 @@
// Do not edit! This is an entry point that provides CommonJS export from ESM
module.exports = require("../adapters/HapiAdapter").default.smart;
"use strict";

const HapiAdapter_1 = require("../adapters/HapiAdapter");

const cjs_ponyfill_1 = require("abortcontroller-polyfill/dist/cjs-ponyfill");

function smart(request, h, storage) {
return new HapiAdapter_1.default({
request,
responseToolkit: h,
storage
}).getSmartApi();
}

smart.AbortController = cjs_ponyfill_1.AbortController;
module.exports = smart;
30 changes: 7 additions & 23 deletions dist/lib/entry/node.d.ts
@@ -1,28 +1,12 @@
/// <reference types="node" />
import { IncomingMessage, ServerResponse } from "http";
import { fhirclient } from "../types";
import Client from "../Client";

export = smart;

type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;


// tslint:disable-next-line: no-namespace
declare type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;
declare function smart(request: IncomingMessage, response: ServerResponse, storage?: fhirclient.Storage | storageFactory): fhirclient.SMART;
declare namespace smart {
export const oauth2: OAuth2;
export function client(stateOrURI: fhirclient.ClientState | string): Client;
}

declare function smart(
req: IncomingMessage,
res: ServerResponse,
storage?: fhirclient.Storage | storageFactory
): OAuth2;

interface OAuth2 {
settings: fhirclient.fhirSettings;
ready: fhirclient.readyFunction;
authorize: (p: fhirclient.AuthorizeParams) => Promise<never>;
init: (p: fhirclient.AuthorizeParams) => Promise<never|Client>;
var AbortController: {
new (): AbortController;
prototype: AbortController;
};
}
export = smart;
18 changes: 16 additions & 2 deletions dist/lib/entry/node.js
@@ -1,2 +1,16 @@
// Do not edit! This is an entry point that provides CommonJS export from ESM
module.exports = require("../adapters/NodeAdapter").default.smart;
"use strict";

const NodeAdapter_1 = require("../adapters/NodeAdapter");

const cjs_ponyfill_1 = require("abortcontroller-polyfill/dist/cjs-ponyfill");

function smart(request, response, storage) {
return new NodeAdapter_1.default({
request,
response,
storage
}).getSmartApi();
}

smart.AbortController = cjs_ponyfill_1.AbortController;
module.exports = smart;
1 change: 1 addition & 0 deletions dist/package.json
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"@types/hapi": "^18.0.3",
"@types/node": "^13.1.4",
"abortcontroller-polyfill": "^1.4.0",
"core-js": "^3.5.0",
"cross-fetch": "^3.0.4",
"debug": "^4.1.1"
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -17,6 +17,7 @@
"browser": "src/browser.js",
"dependencies": {
"@types/hapi": "^18.0.3",
"abortcontroller-polyfill": "^1.4.0",
"core-js": "^3.5.0",
"cross-fetch": "^3.0.4",
"debug": "^4.1.1"
Expand All @@ -27,7 +28,7 @@
"build": "npm run build:module && npm run pack",
"build:dev": "webpack --watch",
"cover": "lab -r lcov -o lcov.info test/**.ts && cat lcov.info | coveralls && rm -rf lcov.info",
"build:module": "cp src/types.d.ts dist/lib && tsc -p tsconfig.module.json && babel --env-name module --ignore dist/lib/entry -d dist/lib dist/lib"
"build:module": "cp src/types.d.ts dist/lib && tsc -p tsconfig.module.json && babel --env-name module -d dist/lib dist/lib"
},
"devDependencies": {
"@babel/cli": "^7.7.5",
Expand Down
17 changes: 14 additions & 3 deletions src/entry/browser.ts
@@ -1,9 +1,16 @@
/* eslint-env browser */

// Note: the following 2 imports appear as unused but they affect how tsc is
// generating type definitions!
import { fhirclient } from "../types";
import Client from "../Client";

// In Browsers we create an adapter, get the SMART api from it and build the
// global FHIR object
import BrowserAdapter from "../adapters/BrowserAdapter";
const { ready, authorize, init, client, options } = BrowserAdapter.smart();

const adapter = new BrowserAdapter();
const { ready, authorize, init, client, options } = adapter.getSmartApi();

// We have two kinds of browser builds - "pure" for new browsers and "legacy"
// for old ones. In pure builds we assume that the browser supports everything
Expand All @@ -14,6 +21,7 @@ const { ready, authorize, init, client, options } = BrowserAdapter.smart();
// eslint-disable-next-line no-undef
if (typeof FHIRCLIENT_PURE == "undefined") {
const fetch = require("cross-fetch");
require("abortcontroller-polyfill/dist/abortcontroller-polyfill-only");
if (!window.fetch) {
window.fetch = fetch.default;
window.Headers = fetch.Headers;
Expand All @@ -22,8 +30,9 @@ if (typeof FHIRCLIENT_PURE == "undefined") {
}
}

// $lab:coverage:off$
export default {
// $lab:coverage:off
const FHIR = {
AbortController: window.AbortController,
client,
oauth2: {
settings: options,
Expand All @@ -32,4 +41,6 @@ export default {
init
}
};

export = FHIR;
// $lab:coverage:on$
23 changes: 22 additions & 1 deletion src/entry/hapi.ts
@@ -1,2 +1,23 @@
import HapiAdapter from "../adapters/HapiAdapter";
export default HapiAdapter.smart;
import { fhirclient } from "../types";
import { AbortController as Controller } from "abortcontroller-polyfill/dist/cjs-ponyfill";
import { ResponseToolkit, Request } from "hapi";

type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;

function smart(
request: Request,
h: ResponseToolkit,
storage?: fhirclient.Storage | storageFactory
)
{
return new HapiAdapter({
request,
responseToolkit: h,
storage
}).getSmartApi();
}

smart.AbortController = Controller as typeof AbortController;

export = smart;
23 changes: 22 additions & 1 deletion src/entry/node.ts
@@ -1,2 +1,23 @@
import NodeAdapter from "../adapters/NodeAdapter";
export default NodeAdapter.smart;
import { IncomingMessage, ServerResponse } from "http";
import { fhirclient } from "../types";
import { AbortController as Controller } from "abortcontroller-polyfill/dist/cjs-ponyfill";

type storageFactory = (options?: fhirclient.JsonObject) => fhirclient.Storage;

function smart(
request: IncomingMessage,
response: ServerResponse,
storage?: fhirclient.Storage | storageFactory
)
{
return new NodeAdapter({
request,
response,
storage
}).getSmartApi();
}

smart.AbortController = Controller as typeof AbortController;

export = smart;

0 comments on commit bddbbd6

Please sign in to comment.