@stripe/ui-extension-sdk
Version:
The suite of functionality available to UI extensions in Stripe apps
81 lines (80 loc) • 3.32 kB
JavaScript
;
/* eslint-disable max-classes-per-file */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AUTHORIZATION_VALUE = exports.AUTHORIZATION_HEADER = exports.createHttpClient = exports.STRIPE_API_KEY = exports.StripeAppsHttpClient = void 0;
/**
* This module provides a HttpClient that can be plugged into stripe-node
* that will allow the user to use stripe-node in extensions if the Dashboard
* provides a `stripeApiFetch` function that will relay API calls through the
* Dashboard and piggy back on the user's Dashboard session.
*/
const invariant_1 = __importDefault(require("invariant"));
const apiFetch_1 = require("./apiFetch");
const matchesStripeKey = /[ps]k_(test|live)_[A-Za-z0-9]+/;
class StripeAppsHttpResponse {
constructor(resp) {
this._resp = resp;
}
getHeaders() {
return this._resp.headers;
}
getStatusCode() {
return this._resp.status;
}
getRawResponse() {
return this._resp;
}
// eslint-disable-next-line class-methods-use-this
toStream() {
throw new Error('Streams have not been implemented in the Stripe HTTP client');
}
// eslint-disable-next-line @typescript-eslint/ban-types
toJSON() {
const { json } = this._resp;
if (json === undefined) {
return Promise.reject(new Error('Response body undefined'));
}
else {
return Promise.resolve(json);
}
}
}
class StripeAppsHttpClient {
constructor(fetch) {
this._fetch = fetch;
}
// eslint-disable-next-line class-methods-use-this
getClientName() {
return 'stripe-ui-extension';
}
async makeRequest(host, port, path, method, headers, requestData, protocol, _timeout) {
(0, invariant_1.default)(protocol === 'https', 'Must use https connections in UI extensions');
const fetchOptions = {
method,
headers,
};
if (requestData) {
fetchOptions.body = requestData;
}
const authHeader = headers.Authorization;
if (authHeader && matchesStripeKey.test(authHeader)) {
throw new Error('Do not use actual stripe keys when using the Stripe JS API client with UI extesions.\n\n Instead, use `STRIPE_API_KEY` from `@stripe/ui-extension-sdk/http_client` as a placeholder.');
}
const url = new URL(path, `${protocol}://${host}`);
const resp = await this._fetch(url.toString(), fetchOptions);
// TODO: Add support for timeouts.
return new StripeAppsHttpResponse(resp);
}
}
exports.StripeAppsHttpClient = StripeAppsHttpClient;
// DO NOT change this string without a deprecation plan. The runtime checks to make sure that this
// exact string is passed, otherwise it will throw an error.
// See: manage/frontend/src/tailor/extensions/host/api_fetch.js
exports.STRIPE_API_KEY = 'DO_NOT_PASS_A_REAL_API_KEY';
const createHttpClient = () => new StripeAppsHttpClient(apiFetch_1.stripeApiFetch);
exports.createHttpClient = createHttpClient;
exports.AUTHORIZATION_HEADER = 'Authorization';
exports.AUTHORIZATION_VALUE = `Bearer ${exports.STRIPE_API_KEY}`;