UNPKG

@wristband/nextjs-auth

Version:

SDK for integrating your NextJS application with Wristband. Handles user authentication and token management.

34 lines (33 loc) 1.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WristbandApiClient = void 0; const error_1 = require("../error"); const constants_1 = require("../utils/constants"); class WristbandApiClient { constructor(wristbandApplicationDomain) { this.baseURL = `https://${wristbandApplicationDomain}/api/v1`; this.defaultHeaders = { 'Content-Type': constants_1.FORM_URLENCODED_MEDIA_TYPE, Accept: constants_1.JSON_MEDIA_TYPE }; } async request(endpoint, options = {}) { const url = `${this.baseURL}${endpoint}`; const headers = { ...this.defaultHeaders, ...options.headers }; const config = { ...options, headers }; const response = await fetch(url, config); if (response.status === 204) { return undefined; } const responseBodyText = await response.text(); const responseBody = responseBodyText ? JSON.parse(responseBodyText) : undefined; if (response.status >= 400) { throw new error_1.FetchError(response, responseBody); } return responseBody; } async get(endpoint, headers = {}) { return this.request(endpoint, { method: 'GET', headers, keepalive: true }); } async post(endpoint, body, headers = {}) { return this.request(endpoint, { method: 'POST', headers, body, keepalive: true }); } } exports.WristbandApiClient = WristbandApiClient;