UNPKG

@point-api/js-sdk

Version:

Javascript SDK for Point API

80 lines 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const account_1 = require("./ApiModules/account"); const autocompleteSession_1 = require("./ApiModules/autocompleteSession"); const snippets_1 = require("./ApiModules/snippets"); const interactions_1 = require("./ApiModules/interactions"); const authManager_1 = require("./authManager"); /** * Point Api Instance */ class PointApiImpl { /** * * @param emailAddress Email address of Point user account * @param apiKey User's API Key * @param apiUrl Point API URL */ constructor(emailAddress, apiKey, apiUrl = "https://v1.pointapi.com") { /** Point API version */ this.ApiVersionAccept = 'application/vnd.point.v1'; this.emailAddress = emailAddress; this.apiUrl = apiUrl; // Init API submodules this.account = new account_1.default(this); this.snippets = new snippets_1.default(this); this.interactions = new interactions_1.default(this); this.authManager = new authManager_1.default(emailAddress, apiKey, apiUrl, this.ApiVersionAccept); } setCredentials(emailAddress, apiKey) { this.emailAddress = emailAddress; this.authManager.setCredentials(emailAddress, apiKey); } /** * Initializes a new autocomplete session. A promise will return if connection is successfully made. * * @param searchType how to search for matching suggestions (standard, keyword, hybdrid) */ async initAutocompleteSessionAsync(searchType) { const session = new autocompleteSession_1.default(this.emailAddress, this.authManager, searchType, this.apiUrl); await session.reconnect(); return session; } /** * Fetches the URL from the server endpoint. * * @param method HTTP method type * @param url Endpoint URL (e.g. /auth or /account) * @param data Payload for the body of the request (e.g. in POST) * @param headers Headers to add to the request */ async authFetch(method, url, data, headers) { const jwt = await this.authManager.getJwt(); if (this.authManager.isUserActive() === false) { throw new Error("User inactive"); } const authHeaders = { Authorization: `Bearer ${jwt}`, ...headers }; return this.fetch(method, url, data, authHeaders); } async fetch(method, url, data, headers) { const { ApiVersionAccept, emailAddress, apiUrl } = this; const body = data ? JSON.stringify(data) : undefined; const fullUrl = `${apiUrl}${url}?emailAddress=${emailAddress}`; const requestHeaders = { Accept: ApiVersionAccept, ...headers }; const response = await fetch(fullUrl, { method, body, headers: requestHeaders, credentials: "include" }); return response; } } exports.default = PointApiImpl; //# sourceMappingURL=main.js.map