autumn-js
Version:
Autumn JS Library
119 lines (115 loc) • 2.93 kB
JavaScript
"use client";
import {
attachMethod,
cancelMethod,
checkMethod,
openBillingPortalMethod,
trackMethod
} from "./chunk-LS3NB4DU.mjs";
import {
createCode,
redeemCode
} from "./chunk-TBCLSIC2.mjs";
import {
toContainerResult
} from "./chunk-OCZFV7OX.mjs";
import {
getPricingTableMethod
} from "./chunk-NZ5PEXT2.mjs";
import {
createCustomerMethod
} from "./chunk-TGX6UKKS.mjs";
import {
createEntityMethod,
deleteEntityMethod,
getEntityMethod
} from "./chunk-4KKWHG2V.mjs";
// src/libraries/react/client/ReactAutumnClient.tsx
var AutumnClient = class {
backendUrl;
getBearerToken;
customerData;
includeCredentials;
constructor({
backendUrl,
getBearerToken,
customerData,
includeCredentials
}) {
this.backendUrl = backendUrl;
this.getBearerToken = getBearerToken;
this.customerData = customerData;
this.includeCredentials = includeCredentials;
}
async getHeaders() {
let headers = {
"Content-Type": "application/json"
};
if (this.getBearerToken) {
try {
let token = await this.getBearerToken();
headers.Authorization = `Bearer ${token}`;
} catch (error) {
console.error(`Failed to call getToken() in AutumnProvider`);
}
}
return headers;
}
async handleFetchResult(result) {
let res = await toContainerResult(result);
return res;
}
async post(path, body) {
const response = await fetch(`${this.backendUrl}${path}`, {
method: "POST",
body: JSON.stringify({
...body,
customer_data: this.customerData || void 0
}),
headers: await this.getHeaders(),
credentials: this.includeCredentials ? "include" : void 0
});
return await this.handleFetchResult(response);
}
async get(path) {
const response = await fetch(`${this.backendUrl}${path}`, {
method: "GET",
headers: await this.getHeaders(),
credentials: this.includeCredentials ? "include" : void 0
});
return await this.handleFetchResult(response);
}
async delete(path) {
const response = await fetch(`${this.backendUrl}${path}`, {
method: "DELETE",
headers: await this.getHeaders()
});
return await this.handleFetchResult(response);
}
async createCustomer(params) {
return await createCustomerMethod({
client: this,
params
});
}
async getPricingTable() {
return await getPricingTableMethod.bind(this)();
}
attach = attachMethod.bind(this);
cancel = cancelMethod.bind(this);
check = checkMethod.bind(this);
track = trackMethod.bind(this);
openBillingPortal = openBillingPortalMethod.bind(this);
entities = {
create: createEntityMethod.bind(this),
get: getEntityMethod.bind(this),
delete: deleteEntityMethod.bind(this)
};
referrals = {
createCode: createCode.bind(this),
redeemCode: redeemCode.bind(this)
};
};
export {
AutumnClient
};