@pfantato/printful-ts
Version:
Typescript SDK to integrate with Printful
96 lines (95 loc) • 4.52 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PrintfulApiService = void 0;
const zod_1 = require("zod");
const ky_1 = require("ky");
const errors_1 = require("@printful-ts/errors");
const schemas_1 = require("@printful-ts/schemas");
const constants_1 = require("@printful-ts/constants");
class PrintfulApiService {
constructor(config) {
const { success, data, error } = schemas_1.PrintfulConfig.safeParse(config);
if (!success) {
throw new errors_1.PrintfulError(error.issues.toLocaleString(), errors_1.PrintfulErrorCode.Enum.SCHEMA_ERROR);
}
this.privateToken = data.privateToken;
this.baseUrl = data.baseUrl;
this.version = data.version;
this.api = this.configure();
}
getBaseUrl() {
if (this.version === 'v1') {
return this.baseUrl;
}
return `${this.baseUrl}/${this.version}`;
}
async isAllowed(scopes) {
var _a;
const { success, data: requiredScopes, error, } = zod_1.z.array(schemas_1.OAuthScopeValue).safeParse(scopes);
if (!success) {
throw new errors_1.PrintfulError(error.issues.toLocaleString(), errors_1.PrintfulErrorCode.Enum.SCHEMA_ERROR);
}
const response = await this.request('/oauth-scopes', {}, schemas_1.GetOAuthScopesResponse);
const allowedScopes = (_a = response.data) === null || _a === void 0 ? void 0 : _a.map(scope => scope.value);
return requiredScopes.every(scope => allowedScopes.includes(scope));
}
configure(_a = {}) {
var { locale: language, store_id, checkScopes } = _a, options = __rest(_a, ["locale", "store_id", "checkScopes"]);
const defaultOptions = Object.assign({}, options, {
headers: Object.assign(Object.assign({}, options.headers), { 'Content-Type': 'application/json' }),
prefixUrl: this.getBaseUrl(),
hooks: Object.assign(Object.assign({}, options.hooks), { beforeRequest: [
async (request) => {
request.headers.set(constants_1.AUTHORIZATION_HEADER, `Bearer ${this.privateToken}`);
if (store_id) {
request.headers.set(constants_1.STORE_ID_HEADER, schemas_1.StoreId.parse(store_id).toString());
}
if (language) {
request.headers.set(constants_1.LANGUAGE_HEADER, schemas_1.Locale.parse(language));
}
if (checkScopes) {
const isAllowed = await this.isAllowed(checkScopes);
if (!isAllowed) {
throw new errors_1.PrintfulError('You are not allowed to perform this action', errors_1.PrintfulErrorCode.Enum.FORBIDDEN);
}
}
},
] }),
});
return ky_1.default.create(defaultOptions);
}
async request(endpoint, requestOptions, schema) {
const _a = requestOptions || {
validateResponseSchema: process.env.PRINTFUL_SCHEMA_VALIDATION
? process.env.PRINTFUL_SCHEMA_VALIDATION === 'true'
: !!schema,
}, { validateResponseSchema: validateSchema } = _a, options = __rest(_a, ["validateResponseSchema"]);
this.api = this.configure({
hooks: {
afterResponse: [
async (_request, _options, response) => {
if (!schema || !validateSchema) {
return response;
}
const jsonResponse = await response.clone().json();
schema.parse(jsonResponse);
return response;
},
],
},
});
return await this.api(endpoint, options).json();
}
}
exports.PrintfulApiService = PrintfulApiService;