@manifoldco/manifold-init
Advanced tools
Comparing version 0.7.3 to 0.7.4
@@ -5,4 +5,4 @@ 'use strict'; | ||
const index = require('./index-68e739cb.js'); | ||
const core = require('./core-ed75bb53.js'); | ||
const index = require('./index-cf4c7d2c.js'); | ||
const core = require('./core-757f07e7.js'); | ||
@@ -44,3 +44,3 @@ const ConnectedButton = class { | ||
version: 0, | ||
componentVersion: '0.7.3', | ||
componentVersion: '0.7.4', | ||
}); | ||
@@ -78,2 +78,3 @@ } | ||
getAuthToken: () => this.authToken, | ||
getOwnerId: () => this.ownerId, | ||
authType: this.authType, | ||
@@ -87,5 +88,22 @@ version, | ||
} | ||
async componentWillLoad() { | ||
const analytics = core.createAnalytics({ | ||
env: this.env, | ||
element: this.el, | ||
componentVersion: '0.7.4', | ||
clientId: this.clientId, | ||
}); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: { | ||
ownerId: this.ownerId || '', | ||
}, | ||
}); | ||
} | ||
render() { | ||
return null; | ||
} | ||
get el() { return index.getElement(this); } | ||
static get watchers() { return { | ||
@@ -92,0 +110,0 @@ "authToken": ["tokenChanged"] |
@@ -5,3 +5,3 @@ 'use strict'; | ||
const core = require('./core-ed75bb53.js'); | ||
const core = require('./core-757f07e7.js'); | ||
@@ -8,0 +8,0 @@ |
@@ -5,8 +5,8 @@ 'use strict'; | ||
const index = require('./index-68e739cb.js'); | ||
const index = require('./index-cf4c7d2c.js'); | ||
const defineCustomElements = (win, options) => index.patchEsm().then(() => { | ||
return index.bootstrapLazy([["connected-button_2.cjs",[[0,"connected-button",{"success":[32],"badRequest":[32],"unauthenticated":[32],"planCost":[32]}],[0,"manifold-init",{"env":[1],"authToken":[1025,"auth-token"],"authType":[1,"auth-type"],"clientId":[1,"client-id"],"initialize":[64]}]]]], options); | ||
return index.bootstrapLazy([["connected-button_2.cjs",[[0,"connected-button",{"success":[32],"badRequest":[32],"unauthenticated":[32],"planCost":[32]}],[0,"manifold-init",{"env":[1],"authToken":[1025,"auth-token"],"authType":[1,"auth-type"],"clientId":[1,"client-id"],"ownerId":[1,"owner-id"],"initialize":[64]}]]]], options); | ||
}); | ||
exports.defineCustomElements = defineCustomElements; |
'use strict'; | ||
const index = require('./index-68e739cb.js'); | ||
const index = require('./index-cf4c7d2c.js'); | ||
index.patchBrowser().then(options => { | ||
return index.bootstrapLazy([["connected-button_2.cjs",[[0,"connected-button",{"success":[32],"badRequest":[32],"unauthenticated":[32],"planCost":[32]}],[0,"manifold-init",{"env":[1],"authToken":[1025,"auth-token"],"authType":[1,"auth-type"],"clientId":[1,"client-id"],"initialize":[64]}]]]], options); | ||
return index.bootstrapLazy([["connected-button_2.cjs",[[0,"connected-button",{"success":[32],"badRequest":[32],"unauthenticated":[32],"planCost":[32]}],[0,"manifold-init",{"env":[1],"authToken":[1025,"auth-token"],"authType":[1,"auth-type"],"clientId":[1,"client-id"],"ownerId":[1,"owner-id"],"initialize":[64]}]]]], options); | ||
}); |
@@ -8,3 +8,3 @@ { | ||
"name": "@stencil/core", | ||
"version": "1.12.4", | ||
"version": "1.12.7", | ||
"typescriptVersion": "3.8.3" | ||
@@ -11,0 +11,0 @@ }, |
@@ -1,3 +0,4 @@ | ||
import { Component, Prop, Method, Event, Watch } from '@stencil/core'; | ||
import { Component, Prop, Method, Element, Event, Watch } from '@stencil/core'; | ||
import * as core from '../../core'; | ||
import createAnalytics from '../../v0/analytics'; | ||
export class ManifoldInit { | ||
@@ -24,2 +25,3 @@ constructor() { | ||
getAuthToken: () => this.authToken, | ||
getOwnerId: () => this.ownerId, | ||
authType: this.authType, | ||
@@ -33,2 +35,18 @@ version, | ||
} | ||
async componentWillLoad() { | ||
const analytics = createAnalytics({ | ||
env: this.env, | ||
element: this.el, | ||
componentVersion: '<@NPM_PACKAGE_VERSION@>', | ||
clientId: this.clientId, | ||
}); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: { | ||
ownerId: this.ownerId || '', | ||
}, | ||
}); | ||
} | ||
render() { | ||
@@ -108,2 +126,19 @@ return null; | ||
"reflect": false | ||
}, | ||
"ownerId": { | ||
"type": "string", | ||
"mutable": false, | ||
"complexType": { | ||
"original": "string", | ||
"resolved": "string", | ||
"references": {} | ||
}, | ||
"required": false, | ||
"optional": true, | ||
"docs": { | ||
"tags": [], | ||
"text": "" | ||
}, | ||
"attribute": "owner-id", | ||
"reflect": false | ||
} | ||
@@ -169,2 +204,3 @@ }; } | ||
}; } | ||
static get elementRef() { return "el"; } | ||
static get watchers() { return [{ | ||
@@ -171,0 +207,0 @@ "propName": "authToken", |
import connection from './v0'; | ||
export function initialize(options) { | ||
const { version, element, env, componentVersion, clientId, getAuthToken, clearAuthToken, } = options; | ||
const { version, element, env, componentVersion, clientId, getAuthToken, clearAuthToken, preview = false, getOwnerId, } = options; | ||
switch (version) { | ||
@@ -14,2 +14,4 @@ case undefined: // latest | ||
clearAuthToken, | ||
preview, | ||
getOwnerId, | ||
}); | ||
@@ -16,0 +18,0 @@ default: |
import { ManifoldError, ErrorType } from './ManifoldError'; | ||
import { RestError } from './RestError'; | ||
import { waitForAuthToken } from '../utils/auth'; | ||
function wait(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
export function createGateway({ baseUrl = () => 'https://api.manifold.co/graphql', retries = 3, }) { | ||
export function createGateway({ baseUrl = () => 'http://api.gateway.arigato.tools/v1', clearAuthToken, getAuthToken, analytics, waitTime = 15000, retries = 3, }) { | ||
async function post(path, body, attempts, init) { | ||
@@ -16,2 +18,3 @@ const opts = init || {}; | ||
catch (e) { | ||
// TODO: Let's figure out if we can safely swallow this. | ||
if (e.name === 'AbortError') { | ||
@@ -24,10 +27,26 @@ throw e; | ||
} | ||
throw new ManifoldError({ type: ErrorType.NetworkError }); | ||
} | ||
if (resp.status === 401) { | ||
// TODO retry auth | ||
return Promise.reject(new ManifoldError({ type: ErrorType.AuthorizationError, message: resp.statusText })); | ||
if (!canRetry) { | ||
throw new ManifoldError({ | ||
type: ErrorType.AuthorizationError, | ||
message: resp.statusText, | ||
}); | ||
} | ||
try { | ||
clearAuthToken(); | ||
return waitForAuthToken(getAuthToken, waitTime, () => post(path, body, attempts + 1, init)); | ||
} | ||
catch (e) { | ||
analytics.report({ | ||
message: e.message, | ||
name: 'manifold-init-error', | ||
}); | ||
throw new ManifoldError({ type: ErrorType.AuthorizationError, message: resp.statusText }); | ||
} | ||
} | ||
const internalServerError = resp.status === 500; | ||
if (internalServerError) { | ||
return Promise.reject(new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText })); | ||
throw new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }); | ||
} | ||
@@ -41,4 +60,7 @@ // Retry on other server errors. | ||
} | ||
return Promise.reject(new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText })); | ||
throw new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }); | ||
} | ||
if (resp.status < 200 || resp.status > 299) { | ||
throw new RestError(resp.status, resp.statusText); | ||
} | ||
return (await resp.json()); | ||
@@ -45,0 +67,0 @@ } |
import { ManifoldError, ErrorType } from './ManifoldError'; | ||
import { waitForAuthToken } from '../utils/auth'; | ||
function wait(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
@@ -19,4 +19,4 @@ // Transforms an HTTP error to fit the GraphQL error format. | ||
}; | ||
const findAuthError = (errors = []) => errors.find(e => e.extensions && e.extensions.type === 'AuthFailed'); | ||
export function createGraphqlFetch({ element, endpoint = () => 'https://api.manifold.co/graphql', getAuthToken, clearAuthToken, version, retries = 3, clientId, analytics, waitTime = 15000, }) { | ||
const findAuthError = (errors = []) => errors.find((e) => e.extensions && e.extensions.type === 'AuthFailed'); | ||
export function createGraphqlFetch({ element, endpoint = () => 'https://api.manifold.co/graphql', getAuthToken, clearAuthToken, version, retries = 3, clientId, analytics, waitTime = 15000, preview, }) { | ||
async function graphqlFetch(args, attempts, init) { | ||
@@ -30,2 +30,10 @@ const opts = init || {}; | ||
} | ||
if (preview) { | ||
if (token) { | ||
console.error('Preview mode cannot be used with an auth token'); | ||
} | ||
else { | ||
options.headers['X-Manifold-Sample'] = 'Platform'; | ||
} | ||
} | ||
if (token) { | ||
@@ -32,0 +40,0 @@ /* eslint-disable-next-line dot-notation */ |
@@ -5,6 +5,16 @@ import createAnalytics from './analytics'; | ||
const connection = (options) => { | ||
const { componentVersion, element, env, clientId, getAuthToken, clearAuthToken } = options; | ||
const { componentVersion, element, env, clientId, getAuthToken, clearAuthToken, preview, getOwnerId, } = options; | ||
const analytics = createAnalytics({ env, element, componentVersion, clientId }); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: {}, | ||
}); | ||
return { | ||
getOwnerId, | ||
gateway: createGateway({ | ||
getAuthToken, | ||
clearAuthToken, | ||
analytics, | ||
baseUrl: () => { | ||
@@ -28,2 +38,3 @@ switch (env) { | ||
analytics, | ||
preview, | ||
endpoint: () => { | ||
@@ -30,0 +41,0 @@ switch (env) { |
@@ -5,2 +5,3 @@ export var ErrorType; | ||
ErrorType["NetworkError"] = "MANIFOLD::NETWORK_ERROR"; | ||
ErrorType["ClientError"] = "MANIFOLD::CLIENT_ERROR"; | ||
ErrorType["ServerError"] = "MANIFOLD::SERVER_ERROR"; | ||
@@ -23,2 +24,4 @@ ErrorType["UnknownError"] = "MANIFOLD::UNKNOWN_ERROR"; | ||
return 'Manifold Error: Server Failed'; | ||
case ErrorType.ClientError: | ||
return this.message; | ||
default: | ||
@@ -25,0 +28,0 @@ return 'Manifold Error: Unknown'; |
@@ -37,4 +37,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { r as registerInstance, h, g as getElement, c as createEvent } from './index-2b32c7ac.js'; | ||
import { i as initialize } from './core-2feb1cea.js'; | ||
import { r as registerInstance, h, g as getElement, c as createEvent } from './index-ce3d5ef8.js'; | ||
import { i as initialize, c as createAnalytics } from './core-24e9473d.js'; | ||
var ConnectedButton = /** @class */ (function () { | ||
@@ -116,3 +116,3 @@ function class_1(hostRef) { | ||
version: 0, | ||
componentVersion: '0.7.3', | ||
componentVersion: '0.7.4', | ||
})]; | ||
@@ -165,2 +165,3 @@ case 2: | ||
getAuthToken: function () { return _this.authToken; }, | ||
getOwnerId: function () { return _this.ownerId; }, | ||
authType: this.authType, | ||
@@ -176,5 +177,32 @@ version: version, | ||
}; | ||
class_2.prototype.componentWillLoad = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var analytics; | ||
return __generator(this, function (_a) { | ||
analytics = createAnalytics({ | ||
env: this.env, | ||
element: this.el, | ||
componentVersion: '0.7.4', | ||
clientId: this.clientId, | ||
}); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: { | ||
ownerId: this.ownerId || '', | ||
}, | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); | ||
}; | ||
class_2.prototype.render = function () { | ||
return null; | ||
}; | ||
Object.defineProperty(class_2.prototype, "el", { | ||
get: function () { return getElement(this); }, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(class_2, "watchers", { | ||
@@ -181,0 +209,0 @@ get: function () { |
@@ -1,3 +0,3 @@ | ||
import { r as registerInstance, h, g as getElement, c as createEvent } from './index-2b32c7ac.js'; | ||
import { i as initialize } from './core-2feb1cea.js'; | ||
import { r as registerInstance, h, g as getElement, c as createEvent } from './index-ce3d5ef8.js'; | ||
import { i as initialize, c as createAnalytics } from './core-24e9473d.js'; | ||
@@ -39,3 +39,3 @@ const ConnectedButton = class { | ||
version: 0, | ||
componentVersion: '0.7.3', | ||
componentVersion: '0.7.4', | ||
}); | ||
@@ -73,2 +73,3 @@ } | ||
getAuthToken: () => this.authToken, | ||
getOwnerId: () => this.ownerId, | ||
authType: this.authType, | ||
@@ -82,5 +83,22 @@ version, | ||
} | ||
async componentWillLoad() { | ||
const analytics = createAnalytics({ | ||
env: this.env, | ||
element: this.el, | ||
componentVersion: '0.7.4', | ||
clientId: this.clientId, | ||
}); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: { | ||
ownerId: this.ownerId || '', | ||
}, | ||
}); | ||
} | ||
render() { | ||
return null; | ||
} | ||
get el() { return getElement(this); } | ||
static get watchers() { return { | ||
@@ -87,0 +105,0 @@ "authToken": ["tokenChanged"] |
@@ -6,3 +6,4 @@ export function applyPolyfills() { | ||
if (!win.customElements || (win.Element && (!win.Element.prototype.closest || !win.Element.prototype.matches || !win.Element.prototype.remove))) { | ||
if (!win.customElements || | ||
(win.Element && (!win.Element.prototype.closest || !win.Element.prototype.matches || !win.Element.prototype.remove || !win.Element.prototype.getRootNode))) { | ||
promises.push(import(/* webpackChunkName: "polyfills-dom" */ './dom.js')); | ||
@@ -9,0 +10,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export{i as initialize}from"./p-27befac8.js"; | ||
export{i as initialize}from"./p-591fdb74.js"; |
@@ -1,1 +0,1 @@ | ||
import{p as t,b as e}from"./p-84762226.js";t().then(t=>e([["p-c5d480c5",[[0,"connected-button",{success:[32],badRequest:[32],unauthenticated:[32],planCost:[32]}],[0,"manifold-init",{env:[1],authToken:[1025,"auth-token"],authType:[1,"auth-type"],clientId:[1,"client-id"],initialize:[64]}]]]],t)); | ||
import{p as t,b as e}from"./p-c1a72367.js";t().then(t=>e([["p-b9a24a9c",[[0,"connected-button",{success:[32],badRequest:[32],unauthenticated:[32],planCost:[32]}],[0,"manifold-init",{env:[1],authToken:[1025,"auth-token"],authType:[1,"auth-type"],clientId:[1,"client-id"],ownerId:[1,"owner-id"],initialize:[64]}]]]],t)); |
@@ -22,2 +22,3 @@ /* eslint-disable */ | ||
}) => Promise<Connection>; | ||
"ownerId"?: string; | ||
} | ||
@@ -53,2 +54,3 @@ } | ||
"onManifold-auth-token-receive"?: (event: CustomEvent<string>) => void; | ||
"ownerId"?: string; | ||
} | ||
@@ -55,0 +57,0 @@ interface IntrinsicElements { |
@@ -6,2 +6,3 @@ import { EventEmitter } from '../../stencil-public-runtime'; | ||
export declare class ManifoldInit { | ||
el: HTMLElement; | ||
env?: 'local' | 'stage' | 'prod'; | ||
@@ -11,2 +12,3 @@ authToken?: string; | ||
clientId?: string; | ||
ownerId?: string; | ||
clear: EventEmitter; | ||
@@ -21,3 +23,4 @@ receive: EventEmitter<string>; | ||
}): Promise<Connection>; | ||
componentWillLoad(): Promise<void>; | ||
render(): any; | ||
} |
@@ -5,3 +5,5 @@ import { Connection as Connection_v0 } from './v0'; | ||
env?: 'local' | 'stage' | 'prod'; | ||
preview?: boolean; | ||
getAuthToken: () => string | undefined; | ||
getOwnerId: () => string | undefined; | ||
clearAuthToken: () => void; | ||
@@ -8,0 +10,0 @@ clientId?: string; |
@@ -15,5 +15,6 @@ export interface Analytics { | ||
/** | ||
* Metric Event Types are events that are sent for observability telemety. | ||
* Based on `name`, what data should be sent? | ||
*/ | ||
export declare type EventTypes = { | ||
export declare type MetricEventTypes = { | ||
name: 'load'; | ||
@@ -45,9 +46,20 @@ properties: { | ||
}; | ||
} | { | ||
}; | ||
/** | ||
* Component Analytics Event Types are events that are sent for customer analytics. | ||
* Based on `name`, what data should be sent? | ||
*/ | ||
export declare type ComponentAnalyticsEventTypes = { | ||
name: 'click'; | ||
properties: Payload; | ||
} | { | ||
name: 'component-load'; | ||
properties: Payload; | ||
}; | ||
export declare type EventEvent = { | ||
type: 'metric' | 'component-analytics'; | ||
} & SharedProperties & EventTypes; | ||
export declare type MetricEvent = { | ||
type: 'metric'; | ||
} & SharedProperties & MetricEventTypes; | ||
export declare type ComponentAnalyticsEvent = { | ||
type: 'component-analytics'; | ||
} & SharedProperties & ComponentAnalyticsEventTypes; | ||
/** | ||
@@ -71,3 +83,3 @@ * Error analytics event | ||
} | ||
export declare type AnalyticsEvent = ErrorEvent | EventEvent; | ||
export declare type AnalyticsEvent = ErrorEvent | MetricEvent | ComponentAnalyticsEvent; | ||
export declare const endpoint: { | ||
@@ -74,0 +86,0 @@ local: string; |
@@ -0,7 +1,14 @@ | ||
import { Analytics } from './analytics'; | ||
export interface Gateway { | ||
post: <Resp, Req extends {}>(path: string, body: Req, init?: RequestInit) => Promise<Resp>; | ||
} | ||
export declare function createGateway({ baseUrl, retries, }: { | ||
interface CreateGateway { | ||
baseUrl?: () => string; | ||
getAuthToken: () => string | undefined; | ||
clearAuthToken: () => void; | ||
retries?: number; | ||
}): Gateway; | ||
waitTime?: number; | ||
analytics: Analytics; | ||
} | ||
export declare function createGateway({ baseUrl, clearAuthToken, getAuthToken, analytics, waitTime, retries, }: CreateGateway): Gateway; | ||
export {}; |
@@ -6,2 +6,3 @@ import { Analytics } from './analytics'; | ||
clearAuthToken: () => void; | ||
preview?: boolean; | ||
clientId?: string; | ||
@@ -41,3 +42,3 @@ element: HTMLElement; | ||
export declare type GraphqlFetch = <T>(args: GraphqlRequest, init?: RequestInit) => Promise<GraphqlResponseBody<T>>; | ||
export declare function createGraphqlFetch({ element, endpoint, getAuthToken, clearAuthToken, version, retries, clientId, analytics, waitTime, }: CreateGraphqlFetch): GraphqlFetch; | ||
export declare function createGraphqlFetch({ element, endpoint, getAuthToken, clearAuthToken, version, retries, clientId, analytics, waitTime, preview, }: CreateGraphqlFetch): GraphqlFetch; | ||
export {}; |
@@ -7,2 +7,3 @@ import { AnalyticsEvent, ErrorDetail } from './analytics'; | ||
gateway: Gateway; | ||
getOwnerId: () => string | undefined; | ||
analytics: { | ||
@@ -18,5 +19,7 @@ track: (e: AnalyticsEvent) => Promise<Response>; | ||
clientId?: string; | ||
preview?: boolean; | ||
getAuthToken: () => string; | ||
clearAuthToken: () => void; | ||
getOwnerId: () => string; | ||
}) => Connection; | ||
export default connection; |
export declare enum ErrorType { | ||
AuthorizationError = "MANIFOLD::AUTHORIZATION_ERROR", | ||
NetworkError = "MANIFOLD::NETWORK_ERROR", | ||
ClientError = "MANIFOLD::CLIENT_ERROR", | ||
ServerError = "MANIFOLD::SERVER_ERROR", | ||
@@ -13,5 +14,5 @@ UnknownError = "MANIFOLD::UNKNOWN_ERROR" | ||
type: ErrorType; | ||
get name(): "Manifold Error: Authorization Failed" | "Manifold Error: Network Call Failed" | "Manifold Error: Server Failed" | "Manifold Error: Unknown"; | ||
get name(): string; | ||
constructor({ message, type }: ErrorOptions); | ||
} | ||
export {}; |
@@ -19,3 +19,3 @@ { | ||
], | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"description": "Manifold UI Initialization", | ||
@@ -56,4 +56,4 @@ "main": "dist/index.js", | ||
"@stencil/core": "^1.8.4", | ||
"@stencil/react-output-target": "0.0.4", | ||
"@types/jest": "24.0.25", | ||
"@stencil/react-output-target": "0.0.6", | ||
"@types/jest": "24.9.1", | ||
"@types/node": "^13.7.0", | ||
@@ -73,3 +73,3 @@ "@types/puppeteer": "2.0.1", | ||
"node-fetch": "^2.6.0", | ||
"prettier": "^1.18.2", | ||
"prettier": "^2.0.4", | ||
"puppeteer": "1.20.0", | ||
@@ -76,0 +76,0 @@ "rollup-plugin-replace": "^2.2.0" |
@@ -22,2 +22,3 @@ /* eslint-disable */ | ||
}) => Promise<Connection>; | ||
"ownerId"?: string; | ||
} | ||
@@ -53,2 +54,3 @@ } | ||
"onManifold-auth-token-receive"?: (event: CustomEvent<string>) => void; | ||
"ownerId"?: string; | ||
} | ||
@@ -55,0 +57,0 @@ interface IntrinsicElements { |
@@ -16,2 +16,3 @@ # mui-core | ||
| `env` | `env` | | `"local" \| "prod" \| "stage"` | `'prod'` | | ||
| `ownerId` | `owner-id` | | `string` | `undefined` | | ||
@@ -18,0 +19,0 @@ |
@@ -6,3 +6,5 @@ import connection, { Connection as Connection_v0 } from './v0'; | ||
env?: 'local' | 'stage' | 'prod'; | ||
preview?: boolean; | ||
getAuthToken: () => string | undefined; | ||
getOwnerId: () => string | undefined; | ||
clearAuthToken: () => void; | ||
@@ -26,2 +28,4 @@ clientId?: string; | ||
clearAuthToken, | ||
preview = false, | ||
getOwnerId, | ||
} = options; | ||
@@ -39,2 +43,4 @@ | ||
clearAuthToken, | ||
preview, | ||
getOwnerId, | ||
}); | ||
@@ -41,0 +47,0 @@ default: |
@@ -124,3 +124,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
expect.assertions(2); | ||
return fetcher({ query: '' }).catch(result => { | ||
return fetcher({ query: '' }).catch((result) => { | ||
expect(fetchMock.called(graphqlEndpoint)).toBe(true); | ||
@@ -160,3 +160,3 @@ expect(result).toEqual( | ||
/* Queue the dispatch back a tick to allow listeners to be set up */ | ||
await new Promise(resolve => { | ||
await new Promise((resolve) => { | ||
setTimeout(() => { | ||
@@ -163,0 +163,0 @@ document.dispatchEvent( |
@@ -18,5 +18,6 @@ export interface Analytics { | ||
/** | ||
* Metric Event Types are events that are sent for observability telemety. | ||
* Based on `name`, what data should be sent? | ||
*/ | ||
export type EventTypes = | ||
export type MetricEventTypes = | ||
| { | ||
@@ -53,13 +54,28 @@ name: 'load'; | ||
}; | ||
} | ||
}; | ||
/** | ||
* Component Analytics Event Types are events that are sent for customer analytics. | ||
* Based on `name`, what data should be sent? | ||
*/ | ||
export type ComponentAnalyticsEventTypes = | ||
| { | ||
name: 'click'; | ||
properties: Payload; | ||
} | ||
| { | ||
name: 'component-load'; | ||
properties: Payload; | ||
}; | ||
export type EventEvent = { | ||
type: 'metric' | 'component-analytics'; | ||
export type MetricEvent = { | ||
type: 'metric'; | ||
} & SharedProperties & | ||
EventTypes; | ||
MetricEventTypes; | ||
export type ComponentAnalyticsEvent = { | ||
type: 'component-analytics'; | ||
} & SharedProperties & | ||
ComponentAnalyticsEventTypes; | ||
/** | ||
@@ -85,3 +101,3 @@ * Error analytics event | ||
export type AnalyticsEvent = ErrorEvent | EventEvent; | ||
export type AnalyticsEvent = ErrorEvent | MetricEvent | ComponentAnalyticsEvent; | ||
@@ -88,0 +104,0 @@ export const endpoint = { |
import { ManifoldError, ErrorType } from './ManifoldError'; | ||
import { RestError } from './RestError'; | ||
import { Analytics } from './analytics'; | ||
import { waitForAuthToken } from '../utils/auth'; | ||
function wait(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
@@ -11,6 +15,19 @@ | ||
interface CreateGateway { | ||
baseUrl?: () => string; | ||
getAuthToken: () => string | undefined; | ||
clearAuthToken: () => void; | ||
retries?: number; | ||
waitTime?: number; | ||
analytics: Analytics; | ||
} | ||
export function createGateway({ | ||
baseUrl = () => 'https://api.manifold.co/graphql', | ||
baseUrl = () => 'http://api.gateway.arigato.tools/v1', | ||
clearAuthToken, | ||
getAuthToken, | ||
analytics, | ||
waitTime = 15000, | ||
retries = 3, | ||
}): Gateway { | ||
}: CreateGateway): Gateway { | ||
async function post<Req extends {}, Resp>( | ||
@@ -41,2 +58,3 @@ path: string, | ||
} catch (e) { | ||
// TODO: Let's figure out if we can safely swallow this. | ||
if (e.name === 'AbortError') { | ||
@@ -50,9 +68,25 @@ throw e; | ||
} | ||
throw new ManifoldError({ type: ErrorType.NetworkError }); | ||
} | ||
if (resp.status === 401) { | ||
// TODO retry auth | ||
return Promise.reject( | ||
new ManifoldError({ type: ErrorType.AuthorizationError, message: resp.statusText }) | ||
); | ||
if (!canRetry) { | ||
throw new ManifoldError({ | ||
type: ErrorType.AuthorizationError, | ||
message: resp.statusText, | ||
}); | ||
} | ||
try { | ||
clearAuthToken(); | ||
return waitForAuthToken(getAuthToken, waitTime, () => post(path, body, attempts + 1, init)); | ||
} catch (e) { | ||
analytics.report({ | ||
message: e.message, | ||
name: 'manifold-init-error', | ||
}); | ||
throw new ManifoldError({ type: ErrorType.AuthorizationError, message: resp.statusText }); | ||
} | ||
} | ||
@@ -62,5 +96,3 @@ | ||
if (internalServerError) { | ||
return Promise.reject( | ||
new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }) | ||
); | ||
throw new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }); | ||
} | ||
@@ -75,7 +107,10 @@ | ||
} | ||
return Promise.reject( | ||
new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }) | ||
); | ||
throw new ManifoldError({ type: ErrorType.ServerError, message: resp.statusText }); | ||
} | ||
if (resp.status < 200 || resp.status > 299) { | ||
throw new RestError(resp.status, resp.statusText); | ||
} | ||
return (await resp.json()) as Resp; | ||
@@ -82,0 +117,0 @@ } |
@@ -6,3 +6,3 @@ import { ManifoldError, ErrorType } from './ManifoldError'; | ||
function wait(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
@@ -26,3 +26,3 @@ | ||
const findAuthError = (errors: GraphqlError[] = []) => | ||
errors.find(e => e.extensions && e.extensions.type === 'AuthFailed'); | ||
errors.find((e) => e.extensions && e.extensions.type === 'AuthFailed'); | ||
@@ -33,2 +33,3 @@ interface CreateGraphqlFetch { | ||
clearAuthToken: () => void; | ||
preview?: boolean; | ||
clientId?: string; | ||
@@ -81,2 +82,3 @@ element: HTMLElement; | ||
waitTime = 15000, | ||
preview, | ||
}: CreateGraphqlFetch): GraphqlFetch { | ||
@@ -108,2 +110,10 @@ async function graphqlFetch<T>( | ||
if (preview) { | ||
if (token) { | ||
console.error('Preview mode cannot be used with an auth token'); | ||
} else { | ||
options.headers['X-Manifold-Sample'] = 'Platform'; | ||
} | ||
} | ||
if (token) { | ||
@@ -190,5 +200,5 @@ /* eslint-disable-next-line dot-notation */ | ||
return function(args: GraphqlRequest, init?: RequestInit) { | ||
return function (args: GraphqlRequest, init?: RequestInit) { | ||
return graphqlFetch(args, 0, init); | ||
}; | ||
} |
@@ -8,2 +8,3 @@ import createAnalytics, { AnalyticsEvent, ErrorDetail } from './analytics'; | ||
gateway: Gateway; | ||
getOwnerId: () => string | undefined; | ||
analytics: { | ||
@@ -20,11 +21,33 @@ track: (e: AnalyticsEvent) => Promise<Response>; | ||
clientId?: string; | ||
preview?: boolean; | ||
getAuthToken: () => string | undefined; | ||
clearAuthToken: () => void; | ||
getOwnerId: () => string | undefined; | ||
}): Connection => { | ||
const { componentVersion, element, env, clientId, getAuthToken, clearAuthToken } = options; | ||
const { | ||
componentVersion, | ||
element, | ||
env, | ||
clientId, | ||
getAuthToken, | ||
clearAuthToken, | ||
preview, | ||
getOwnerId, | ||
} = options; | ||
const analytics = createAnalytics({ env, element, componentVersion, clientId }); | ||
analytics.track({ | ||
description: 'Track component load event', | ||
name: 'component-load', | ||
type: 'component-analytics', | ||
properties: {}, | ||
}); | ||
return { | ||
getOwnerId, | ||
gateway: createGateway({ | ||
getAuthToken, | ||
clearAuthToken, | ||
analytics, | ||
baseUrl: () => { | ||
@@ -48,2 +71,3 @@ switch (env) { | ||
analytics, | ||
preview, | ||
endpoint: () => { | ||
@@ -50,0 +74,0 @@ switch (env) { |
export enum ErrorType { | ||
AuthorizationError = 'MANIFOLD::AUTHORIZATION_ERROR', | ||
NetworkError = 'MANIFOLD::NETWORK_ERROR', | ||
ClientError = 'MANIFOLD::CLIENT_ERROR', | ||
ServerError = 'MANIFOLD::SERVER_ERROR', | ||
@@ -23,2 +24,4 @@ UnknownError = 'MANIFOLD::UNKNOWN_ERROR', | ||
return 'Manifold Error: Server Failed'; | ||
case ErrorType.ClientError: | ||
return this.message; | ||
default: | ||
@@ -25,0 +28,0 @@ return 'Manifold Error: Unknown'; |
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
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 too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
800794
118
11216