From 5f9745c0b1666253c01ae9d8dc0ec58be08a6ec4 Mon Sep 17 00:00:00 2001 From: Hob Adams Date: Wed, 3 Jul 2024 22:42:43 +0100 Subject: [PATCH] improve package output --- .changeset/wise-squids-teach.md | 5 + package.json | 3 + src/HttpClient.js | 198 -------------------------------- src/index.js | 17 --- 4 files changed, 8 insertions(+), 215 deletions(-) create mode 100644 .changeset/wise-squids-teach.md delete mode 100644 src/HttpClient.js delete mode 100644 src/index.js diff --git a/.changeset/wise-squids-teach.md b/.changeset/wise-squids-teach.md new file mode 100644 index 0000000..fe89650 --- /dev/null +++ b/.changeset/wise-squids-teach.md @@ -0,0 +1,5 @@ +--- +"@hobadams/nextjs-http-client": patch +--- + +add dist diff --git a/package.json b/package.json index 44143f8..dc7fbfd 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "publishConfig": { "access": "public" }, + "files": [ + "dist/" + ], "scripts": { "build": "tsup src/index.ts --format cjs,esm --minify --dts", "release": "pnpm changeset version && pnpm changeset publish --access public", diff --git a/src/HttpClient.js b/src/HttpClient.js deleted file mode 100644 index ea91683..0000000 --- a/src/HttpClient.js +++ /dev/null @@ -1,198 +0,0 @@ -"use strict"; -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ConsoleLogger = exports.HttpClient = void 0; -class ConsoleLogger { - log(message) { - console.error(message); - } -} -exports.ConsoleLogger = ConsoleLogger; -class HttpClient { - constructor(config) { - this.headers = {}; - this.defaultCache = 'default'; - this.customErrorMessages = {}; - this.baseUrl = config.baseUrl; - if (config.headers) { - this.headers = config.headers; - } - if (config.cache) { - this.defaultCache = config.cache; - } - if (config.customErrorMessages) { - this.customErrorMessages = config.customErrorMessages; - } - this.logger = config.logger || new ConsoleLogger(); - } - request(url, options, config) { - return __awaiter(this, void 0, void 0, function* () { - const queryString = (config === null || config === void 0 ? void 0 : config.params) - ? this.buildQueryString(config.params) - : ''; - const fullUrl = this.baseUrl + url + queryString; - const response = yield fetch(fullUrl, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign(Object.assign({}, this.headers), config === null || config === void 0 ? void 0 : config.headers), options.headers), cache: (config === null || config === void 0 ? void 0 : config.cache) || this.defaultCache })); - const contentType = response.headers.get('Content-Type'); - if (!response.ok) { - const displayErrorMessage = this.customErrorMessages[response.status] || - this.defaultErrorMessage(response.status); - let errorMessage = displayErrorMessage; - if (contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json')) { - const error = yield response.json(); - errorMessage = error.message || errorMessage; - } - else { - errorMessage = (yield response.text()) || errorMessage; - } - this.logger.log(`Error ${response.status}: ${errorMessage}`); - throw new Error(displayErrorMessage); - } - if (contentType === null || contentType === void 0 ? void 0 : contentType.includes('application/json')) { - return response.json(); - } - else if (contentType === null || contentType === void 0 ? void 0 : contentType.includes('text')) { - return response.text(); - } - else { - return response.blob(); - } - }); - } - serializeBody(body) { - return JSON.stringify(body); - } - buildQueryString(params) { - const queryString = new URLSearchParams(params).toString(); - return queryString ? `?${queryString}` : ''; - } - defaultErrorMessage(status) { - switch (status) { - case 400: - return 'Bad Request'; - case 401: - return 'Unauthorized'; - case 402: - return 'Payment Required'; - case 403: - return 'Forbidden'; - case 404: - return 'Not Found'; - case 405: - return 'Method Not Allowed'; - case 406: - return 'Not Acceptable'; - case 407: - return 'Proxy Authentication Required'; - case 408: - return 'Request Timeout'; - case 409: - return 'Conflict'; - case 410: - return 'Gone'; - case 411: - return 'Length Required'; - case 412: - return 'Precondition Failed'; - case 413: - return 'Payload Too Large'; - case 414: - return 'URI Too Long'; - case 415: - return 'Unsupported Media Type'; - case 416: - return 'Range Not Satisfiable'; - case 417: - return 'Expectation Failed'; - case 421: - return 'Misdirected Request'; - case 422: - return 'Unprocessable Entity'; - case 423: - return 'Locked'; - case 424: - return 'Failed Dependency'; - case 425: - return 'Too Early'; - case 426: - return 'Upgrade Required'; - case 428: - return 'Precondition Required'; - case 429: - return 'Too Many Requests'; - case 431: - return 'Request Header Fields Too Large'; - case 451: - return 'Unavailable For Legal Reasons'; - case 500: - return 'Internal Service Error'; - case 501: - return 'Not Implemented'; - case 502: - return 'Bad Gateway'; - case 503: - return 'Service Unavailable'; - case 504: - return 'Gateway Timeout'; - default: - return 'An error occurred'; - } - } - static create(config) { - return new HttpClient(config); - } - setHeaders(headers) { - this.headers = headers; - } - get(url, config) { - return this.request(url, { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }, config); - } - post(url, config) { - return this.request(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: (config === null || config === void 0 ? void 0 : config.body) ? this.serializeBody(config.body) : undefined, - }, config); - } - put(url, config) { - return this.request(url, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - }, - body: (config === null || config === void 0 ? void 0 : config.body) ? this.serializeBody(config.body) : undefined, - }, config); - } - patch(url, config) { - return this.request(url, { - method: 'PATCH', - headers: { - 'Content-Type': 'application/json', - }, - body: (config === null || config === void 0 ? void 0 : config.body) ? this.serializeBody(config.body) : undefined, - }, config); - } - delete(url, config) { - return this.request(url, { - method: 'DELETE', - headers: { - 'Content-Type': 'application/json', - }, - }, config); - } -} -exports.HttpClient = HttpClient; diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 6c09602..0000000 --- a/src/index.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -__exportStar(require("./HttpClient"), exports);