@ingrain/ingrain-client-js
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -10,7 +10,11 @@ export declare enum Action { | ||
actorDisplayName: string; | ||
actorMetadata: any; | ||
actorMetadata: { | ||
[key: string]: string; | ||
} | null; | ||
entityId: string; | ||
entityName: string; | ||
entityDisplayLabel: string; | ||
entityMetadata: any; | ||
entityMetadata: { | ||
[key: string]: string; | ||
} | null; | ||
entityPropName: string | null; | ||
@@ -28,7 +32,11 @@ appUserOrgId: string | null; | ||
actorDisplayName: string; | ||
actorMetadata: any; | ||
actorMetadata: { | ||
[key: string]: string; | ||
} | null; | ||
entityId: string; | ||
entityName: string; | ||
entityDisplayLabel: string; | ||
entityMetadata: any; | ||
entityMetadata: { | ||
[key: string]: string; | ||
} | null; | ||
entityPropName: string | null; | ||
@@ -35,0 +43,0 @@ appUserOrgId: string | null; |
@@ -8,3 +8,3 @@ type GetOptions = { | ||
type Hooks = { | ||
beforeRequest?: Array<(request: RequestInit) => Promise<void>>; | ||
beforeRequest?: Array<(request: RequestInit) => void>; | ||
}; | ||
@@ -11,0 +11,0 @@ type InitOptions = { |
@@ -28,3 +28,3 @@ "use strict"; | ||
if (this._hooks?.beforeRequest?.length) { | ||
await Promise.all(this._hooks?.beforeRequest?.map(async (hook) => await hook(request))); | ||
this._hooks?.beforeRequest?.forEach((hook) => hook(request)); | ||
} | ||
@@ -31,0 +31,0 @@ const response = await fetch(finalUrl, request); |
import Ingrain from './ingrain'; | ||
declare const singletonInstance: Ingrain; | ||
export default singletonInstance; | ||
export * from './events'; | ||
export type { IngrainOpts } from './ingrain'; | ||
export { Ingrain }; |
@@ -23,3 +23,5 @@ "use strict"; | ||
exports.Ingrain = ingrain_1.default; | ||
const singletonInstance = ingrain_1.default.getInstance(); | ||
exports.default = singletonInstance; | ||
__exportStar(require("./events"), exports); | ||
//# sourceMappingURL=index.js.map |
import { AppUserEventResp, CreateAppUserEvent, ReadDisplayableAppUserEventsReqQuery, ReadDisplayableAppUserEventsResp } from './events'; | ||
export type GetUserJwtTokenCallback = () => Promise<string>; | ||
export type GetUserJwtTokenCallback = () => string; | ||
export type IngrainOpts = { | ||
apiUrl?: string; | ||
apiVersion?: string; | ||
publishableKey: string; | ||
publishableKey?: string; | ||
getUserJwtToken?: GetUserJwtTokenCallback; | ||
@@ -16,3 +16,5 @@ }; | ||
private _sdkPrefix; | ||
constructor(ingrainOpts: IngrainOpts); | ||
static _singletonInstance: Ingrain; | ||
constructor(ingrainOpts?: IngrainOpts); | ||
static getInstance(): Ingrain; | ||
setGetUserJwtTokenCallback(getUserJwtToken: GetUserJwtTokenCallback): void; | ||
@@ -19,0 +21,0 @@ createUserEvent(createUserEventBody: CreateAppUserEvent): Promise<AppUserEventResp>; |
@@ -1,77 +0,83 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const http_client_1 = require("./http-client"); | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
const http_client_1 = require('./http-client'); | ||
const AUTHORIZATION_HEADER = 'Authorization'; | ||
const INGRAIN_LOCAL_STORAGE_USER_JWT_TOKEN_KEY = 'ingrain_user_jwt_token'; | ||
class Ingrain { | ||
constructor(ingrainOpts) { | ||
this._sdkPrefix = 'sdk'; | ||
this._apiUrl = ingrainOpts.apiUrl || 'https://api-prod.ingrain.dev'; | ||
this._apiVersion = ingrainOpts.apiVersion || 'v1'; | ||
if (!ingrainOpts.publishableKey?.trim()) { | ||
throw new Error('Publishable key is required'); | ||
} | ||
this._publishableKey = ingrainOpts.publishableKey.trim(); | ||
this._getUserJwtToken = | ||
ingrainOpts.getUserJwtToken || | ||
(async () => { | ||
if (typeof window !== 'undefined') { | ||
return localStorage.getItem(INGRAIN_LOCAL_STORAGE_USER_JWT_TOKEN_KEY); | ||
} | ||
else { | ||
throw new Error(`localStorage is not defined. The client-side SDK is meant to be used in the browser. | ||
constructor(ingrainOpts) { | ||
console.log({ ingrainOpts }); | ||
this._sdkPrefix = 'sdk'; | ||
this._apiUrl = ingrainOpts?.apiUrl || 'https://api-prod.ingrain.dev'; | ||
this._apiVersion = ingrainOpts?.apiVersion || 'v1'; | ||
this._publishableKey = ingrainOpts?.publishableKey || ''; | ||
this._getUserJwtToken = | ||
ingrainOpts?.getUserJwtToken || | ||
(() => { | ||
if (typeof window !== 'undefined') { | ||
return localStorage.getItem(INGRAIN_LOCAL_STORAGE_USER_JWT_TOKEN_KEY); | ||
} else { | ||
throw new Error(`localStorage is not defined. The client-side SDK is meant to be used in the browser. | ||
Please make sure your component is mounted first. For example, if you're using React, | ||
call the client function in a useEffect().`); | ||
} | ||
}); | ||
this._httpClient = new http_client_1.HttpClient({ | ||
prefixUrl: new URL(`api/${this._apiVersion}/${this._sdkPrefix}`, this._apiUrl), | ||
hooks: { | ||
beforeRequest: [ | ||
async (request) => { | ||
if (!request.headers) { | ||
request.headers = {}; | ||
} | ||
request.headers = { | ||
...request.headers, | ||
'x-ingrain-publishable-key': this._publishableKey, | ||
}; | ||
const userJwtToken = await this._getUserJwtToken(); | ||
if (userJwtToken) { | ||
request.headers = { | ||
...request.headers, | ||
[AUTHORIZATION_HEADER]: `Bearer ${userJwtToken}`, | ||
}; | ||
} | ||
else { | ||
throw new Error('User JWT token is required'); | ||
} | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
}); | ||
this._httpClient = new http_client_1.HttpClient({ | ||
prefixUrl: new URL( | ||
`api/${this._apiVersion}/${this._sdkPrefix}`, | ||
this._apiUrl, | ||
), | ||
hooks: { | ||
beforeRequest: [ | ||
(request) => { | ||
if (!request.headers) { | ||
request.headers = {}; | ||
} | ||
request.headers = { | ||
...request.headers, | ||
'x-ingrain-publishable-key': this._publishableKey, | ||
}; | ||
const userJwtToken = this._getUserJwtToken(); | ||
console.log('aaasss', { userJwtToken }, this); | ||
if (userJwtToken) { | ||
request.headers = { | ||
...request.headers, | ||
[AUTHORIZATION_HEADER]: `Bearer ${userJwtToken}`, | ||
}; | ||
} else { | ||
throw new Error('User JWT token is required'); | ||
} | ||
}, | ||
], | ||
}, | ||
}); | ||
} | ||
static getInstance() { | ||
if (!this._singletonInstance) { | ||
this._singletonInstance = new Ingrain(); | ||
} | ||
setGetUserJwtTokenCallback(getUserJwtToken) { | ||
this._getUserJwtToken = getUserJwtToken; | ||
} | ||
async createUserEvent(createUserEventBody) { | ||
return await this._makePostRequest({ | ||
endpoint: 'app-user-events', | ||
body: createUserEventBody, | ||
}); | ||
} | ||
async readUserEvents(readQuery) { | ||
return await this._makeGetRequest({ | ||
endpoint: 'app-user-events', | ||
query: readQuery, | ||
}); | ||
} | ||
async _makePostRequest({ endpoint, body, }) { | ||
return await this._httpClient.post(endpoint, { json: body }); | ||
} | ||
async _makeGetRequest({ endpoint, query, }) { | ||
return await this._httpClient.get(endpoint, { searchParams: query }); | ||
} | ||
return this._singletonInstance; | ||
} | ||
setGetUserJwtTokenCallback(getUserJwtToken) { | ||
this._getUserJwtToken = getUserJwtToken; | ||
} | ||
async createUserEvent(createUserEventBody) { | ||
return await this._makePostRequest({ | ||
endpoint: 'app-user-events', | ||
body: createUserEventBody, | ||
}); | ||
} | ||
async readUserEvents(readQuery) { | ||
return await this._makeGetRequest({ | ||
endpoint: 'app-user-events', | ||
query: readQuery, | ||
}); | ||
} | ||
async _makePostRequest({ endpoint, body }) { | ||
return await this._httpClient.post(endpoint, { json: body }); | ||
} | ||
async _makeGetRequest({ endpoint, query }) { | ||
return await this._httpClient.get(endpoint, { searchParams: query }); | ||
} | ||
} | ||
exports.default = Ingrain; | ||
//# sourceMappingURL=ingrain.js.map | ||
//# sourceMappingURL=ingrain.js.map |
{ | ||
"name": "@ingrain/ingrain-client-js", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Ingrain (ingrain.dev) client-side SDK", | ||
@@ -23,3 +23,4 @@ "homepage": "https://ingrain.dev", | ||
"clean": "rm -rf dist", | ||
"rebuild": "yarn clean && yarn build" | ||
"rebuild": "yarn clean && yarn build", | ||
"publish": "npm publish --access public" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
@@ -27,8 +27,60 @@ # Ingrain Client SDK | ||
``` | ||
```sh | ||
yarn add @ingrain/ingrain-client-js | ||
``` | ||
## Usage | ||
```ts | ||
import { Ingrain } from '@ingrain/ingrain-client-js'; | ||
// Initialize Ingrain | ||
const opts = { | ||
publishableKey: 'your-publishable-key', | ||
getUserJwtToken: async () => { | ||
return "your-user's-jwt-token"; | ||
}, | ||
}; | ||
const ingrain = new Ingrain(opts); | ||
// Log action | ||
ingrain | ||
.createUserEvent({ | ||
actorId: 'example-actor-id', | ||
actorDisplayName: 'example-actor-id', | ||
actorMetadata: { | ||
// Can also be null for no metadata | ||
any: 'any', | ||
metadata: 'metadata', | ||
here: 'here', | ||
}, | ||
entityId: 'example-application', | ||
entityName: 'Example Application', | ||
entityDisplayLabel: `Example Application's settings`, | ||
entityMetadata: null, | ||
entityPropName: 'Settings', // Can be null | ||
appUserOrgId: 'example-org-id', | ||
action: Action.UPDATE, | ||
oldValue: 'old-settings-value', // can be null | ||
newValue: 'new-settings-value', // can be null | ||
}) | ||
.catch((err) => { | ||
console.error('Error creating user event', err); | ||
}); | ||
// Get data | ||
ingrain | ||
.readUserEvents({ | ||
actorId: 'example-actor-id', | ||
}) | ||
.then((resp) => { | ||
const { data } = resp; | ||
// Display the data | ||
}); | ||
``` | ||
## Documentation | ||
You can start from [Introduction](https://docs.ingrain.dev/docs/category/getting-started) or jump straight to [Client JS SDK](https://docs.ingrain.dev/docs/category/client-javascript-sdk) | ||
You can start from [Introduction](https://docs.ingrain.dev) or jump straight to [Client JS SDK](https://docs.ingrain.dev/category/client-javascript-sdk) |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56626
273
86
2