@mintlify/http-client 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,16 @@
1
- # @mintlify/htt-clients
1
+ # @mintlify/http-clients
2
2
 
3
- Reusable Mintlify http clients. See [mintlify](../mintlify/README.md) for more documentation.
3
+ Reusable Mintlify http clients [npm package](https://www.npmjs.com/package/@mintlify/http-client). See [mintlify](../../README.md) for more documentation.
4
4
 
5
+ ## Installation
6
+
7
+ ```
8
+ npm install @mintlify-http-client
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ const client = getLeavesClient(subdomain);
15
+ navPromise = client.getNavigation();
16
+ ```
@@ -1,154 +1,70 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
- return new (P || (P = Promise))(function (resolve, reject) {
15
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
- step((generator = generator.apply(thisArg, _arguments || [])).next());
19
- });
20
- };
21
- var __generator = (this && this.__generator) || function (thisArg, body) {
22
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
- function verb(n) { return function (v) { return step([n, v]); }; }
25
- function step(op) {
26
- if (f) throw new TypeError("Generator is already executing.");
27
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
- if (y = 0, t) op = [op[0] & 2, t.value];
30
- switch (op[0]) {
31
- case 0: case 1: t = op; break;
32
- case 4: _.label++; return { value: op[1], done: false };
33
- case 5: _.label++; y = op[1]; op = [0]; continue;
34
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
- default:
36
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
- if (t[2]) _.ops.pop();
41
- _.trys.pop(); continue;
42
- }
43
- op = body.call(thisArg, _);
44
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
- }
47
- };
48
1
  import { handleJson, handleText } from './clientError';
49
2
  /**
50
3
  * BaseClient serves as the base class for API clients.
51
4
  * Provides basic functionalities for making HTTP requests to a server.
52
5
  */
53
- var BaseClient = /** @class */ (function () {
6
+ export class BaseClient {
54
7
  /**
55
8
  * @param host - The server's address.
56
9
  * @param reqInit - Optional default request initializer.
57
10
  */
58
- function BaseClient(host, reqInit) {
59
- if (reqInit === void 0) { reqInit = undefined; }
11
+ constructor(host, reqInit = undefined) {
60
12
  this.host = host;
61
13
  this.reqInit = reqInit;
62
14
  }
63
- BaseClient.prototype.fetch = function (path, query, init) {
64
- var _a;
65
- return __awaiter(this, void 0, void 0, function () {
66
- var req, uri;
67
- var _b;
68
- return __generator(this, function (_c) {
69
- switch (_c.label) {
70
- case 0:
71
- req = __assign(__assign(__assign({}, this.reqInit), init), { headers: __assign(__assign({}, (_a = this.reqInit) === null || _a === void 0 ? void 0 : _a.headers), init === null || init === void 0 ? void 0 : init.headers) });
72
- uri = this.getUri(path);
73
- uri = query ? "".concat(uri, "?").concat(new URLSearchParams(query)) : uri;
74
- _b = { uri: uri };
75
- return [4 /*yield*/, fetch(uri, req)];
76
- case 1: return [2 /*return*/, (_b.res = _c.sent(), _b.req = req, _b)];
77
- }
78
- });
79
- });
80
- };
81
- BaseClient.prototype.get = function (path, query, init) {
15
+ async fetch(path, query, init) {
16
+ const req = {
17
+ ...this.reqInit,
18
+ ...init,
19
+ headers: {
20
+ ...this.reqInit?.headers,
21
+ ...init?.headers,
22
+ },
23
+ };
24
+ let uri = this.getUri(path);
25
+ uri = query ? `${uri}?${new URLSearchParams(query)}` : uri;
26
+ return { uri, res: await fetch(uri, req), req };
27
+ }
28
+ get(path, query, init) {
82
29
  return this.method('get', path, query, init);
83
- };
84
- BaseClient.prototype.put = function (path, query, init) {
30
+ }
31
+ put(path, query, init) {
85
32
  return this.method('put', path, query, init);
86
- };
87
- BaseClient.prototype.delete = function (path, query, init) {
33
+ }
34
+ delete(path, query, init) {
88
35
  return this.method('delete', path, query, init);
89
- };
90
- BaseClient.prototype.post = function (path, query, init) {
36
+ }
37
+ post(path, query, init) {
91
38
  return this.method('post', path, query, init);
92
- };
93
- BaseClient.prototype.text = function (path, query, init) {
94
- return __awaiter(this, void 0, void 0, function () {
95
- var result;
96
- return __generator(this, function (_a) {
97
- switch (_a.label) {
98
- case 0: return [4 /*yield*/, this.fetch(path, query, init)];
99
- case 1:
100
- result = _a.sent();
101
- return [2 /*return*/, this.handleText(result)];
102
- }
103
- });
104
- });
105
- };
106
- BaseClient.prototype.json = function (path, query, init) {
107
- return __awaiter(this, void 0, void 0, function () {
108
- var result;
109
- return __generator(this, function (_a) {
110
- switch (_a.label) {
111
- case 0: return [4 /*yield*/, this.fetch(path, query, init)];
112
- case 1:
113
- result = _a.sent();
114
- return [2 /*return*/, this.handleJson(result)];
115
- }
116
- });
117
- });
118
- };
119
- BaseClient.prototype.method = function (method, path, query, init) {
120
- return __awaiter(this, void 0, void 0, function () {
121
- var result;
122
- return __generator(this, function (_a) {
123
- switch (_a.label) {
124
- case 0: return [4 /*yield*/, this.fetch(path, query, __assign(__assign({}, init), { method: method }))];
125
- case 1:
126
- result = _a.sent();
127
- return [2 /*return*/, result.res];
128
- }
129
- });
130
- });
131
- };
39
+ }
40
+ async text(path, query, init) {
41
+ const result = await this.fetch(path, query, init);
42
+ return this.handleText(result);
43
+ }
44
+ async json(path, query, init) {
45
+ const result = await this.fetch(path, query, init);
46
+ return this.handleJson(result);
47
+ }
48
+ async method(method, path, query, init) {
49
+ const result = await this.fetch(path, query, { ...init, method });
50
+ return result.res;
51
+ }
132
52
  /**
133
53
  * Constructs a URI path by joining URI components.
134
54
  * @param paths - Paths to encode and join.
135
55
  * @returns encoded URI path.
136
56
  */
137
- BaseClient.prototype.getPath = function () {
138
- var paths = [];
139
- for (var _i = 0; _i < arguments.length; _i++) {
140
- paths[_i] = arguments[_i];
141
- }
57
+ getPath(...paths) {
142
58
  return paths.map(encodeURIComponent).join('/');
143
- };
59
+ }
144
60
  /**
145
61
  * Constructs a URI by appending a path to the host.
146
62
  * @param path - The path to append to the host.
147
63
  * @returns URI string.
148
64
  */
149
- BaseClient.prototype.getUri = function (path) {
150
- return encodeURI("".concat(this.host, "/").concat(path));
151
- };
65
+ getUri(path) {
66
+ return encodeURI(`${this.host}/${path}`);
67
+ }
152
68
  /**
153
69
  * Handles text responses from the server.
154
70
  * @param req - The request object.
@@ -156,10 +72,9 @@ var BaseClient = /** @class */ (function () {
156
72
  * @param uri - The request URI.
157
73
  * @returns Promise that resolves with the text data.
158
74
  */
159
- BaseClient.prototype.handleText = function (_a) {
160
- var req = _a.req, res = _a.res, uri = _a.uri;
75
+ handleText({ req, res, uri, }) {
161
76
  return handleText.bind(this)(req, res, uri);
162
- };
77
+ }
163
78
  /**
164
79
  * Handles JSON responses from the server.
165
80
  * @param req - The request object.
@@ -167,11 +82,8 @@ var BaseClient = /** @class */ (function () {
167
82
  * @param uri - The request URI.
168
83
  * @returns Promise that resolves with the JSON data.
169
84
  */
170
- BaseClient.prototype.handleJson = function (_a) {
171
- var req = _a.req, res = _a.res, uri = _a.uri;
172
- var handle = (handleJson);
85
+ handleJson({ req, res, uri, }) {
86
+ const handle = (handleJson);
173
87
  return handle.bind(this)(req, res, uri);
174
- };
175
- return BaseClient;
176
- }());
177
- export { BaseClient };
88
+ }
89
+ }
@@ -1,60 +1,8 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = function (d, b) {
3
- extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
6
- return extendStatics(d, b);
7
- };
8
- return function (d, b) {
9
- if (typeof b !== "function" && b !== null)
10
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
11
- extendStatics(d, b);
12
- function __() { this.constructor = d; }
13
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
14
- };
15
- })();
16
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
- return new (P || (P = Promise))(function (resolve, reject) {
19
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
- step((generator = generator.apply(thisArg, _arguments || [])).next());
23
- });
24
- };
25
- var __generator = (this && this.__generator) || function (thisArg, body) {
26
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28
- function verb(n) { return function (v) { return step([n, v]); }; }
29
- function step(op) {
30
- if (f) throw new TypeError("Generator is already executing.");
31
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
32
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33
- if (y = 0, t) op = [op[0] & 2, t.value];
34
- switch (op[0]) {
35
- case 0: case 1: t = op; break;
36
- case 4: _.label++; return { value: op[1], done: false };
37
- case 5: _.label++; y = op[1]; op = [0]; continue;
38
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
39
- default:
40
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44
- if (t[2]) _.ops.pop();
45
- _.trys.pop(); continue;
46
- }
47
- op = body.call(thisArg, _);
48
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50
- }
51
- };
52
- import { ACCESS_DENIED, UNAUTHORIZED } from '../leaves-client/constants';
1
+ import { ACCESS_DENIED, UNAUTHORIZED } from '../leaves-client';
53
2
  /**
54
3
  * Represents a client-side error.
55
4
  */
56
- var ClientError = /** @class */ (function (_super) {
57
- __extends(ClientError, _super);
5
+ class ClientError extends Error {
58
6
  /**
59
7
  * Constructs a ClientError.
60
8
  * @param message - The error message.
@@ -63,16 +11,14 @@ var ClientError = /** @class */ (function (_super) {
63
11
  * @param request - The original request.
64
12
  * @param uri - The URI to which the request was sent.
65
13
  */
66
- function ClientError(message, client, response, request, uri) {
67
- var _this = _super.call(this, message) || this;
68
- _this.client = client;
69
- _this.response = response;
70
- _this.request = request;
71
- _this.uri = uri;
72
- return _this;
14
+ constructor(message, client, response, request, uri) {
15
+ super(message);
16
+ this.client = client;
17
+ this.response = response;
18
+ this.request = request;
19
+ this.uri = uri;
73
20
  }
74
- return ClientError;
75
- }(Error));
21
+ }
76
22
  /**
77
23
  * Type guard for ClientError.
78
24
  * @param object - The object to check.
@@ -98,24 +44,14 @@ export function isClientError(object) {
98
44
  * @throws ClientError - If the response is not ok.
99
45
  * @returns The fetched text.
100
46
  */
101
- export function handleText(req, res, uri) {
102
- return __awaiter(this, void 0, void 0, function () {
103
- var text;
104
- return __generator(this, function (_a) {
105
- switch (_a.label) {
106
- case 0: return [4 /*yield*/, res.text()];
107
- case 1:
108
- text = _a.sent();
109
- if (res.ok) {
110
- return [2 /*return*/, text];
111
- }
112
- else {
113
- throw new ClientError(text, this, res, req, uri);
114
- }
115
- return [2 /*return*/];
116
- }
117
- });
118
- });
47
+ export async function handleText(req, res, uri) {
48
+ const text = await res.text();
49
+ if (res.ok) {
50
+ return text;
51
+ }
52
+ else {
53
+ throw new ClientError(text, this, res, req, uri);
54
+ }
119
55
  }
120
56
  /**
121
57
  * Fetches a resource as JSON.
@@ -125,21 +61,13 @@ export function handleText(req, res, uri) {
125
61
  * @throws ClientError - If the response is not ok.
126
62
  * @returns The fetched JSON data.
127
63
  */
128
- export function handleJson(req, res, uri) {
129
- return __awaiter(this, void 0, void 0, function () {
130
- var _a;
131
- return __generator(this, function (_b) {
132
- switch (_b.label) {
133
- case 0:
134
- if (!res.ok) return [3 /*break*/, 1];
135
- return [2 /*return*/, res.json()];
136
- case 1:
137
- _a = ClientError.bind;
138
- return [4 /*yield*/, res.text()];
139
- case 2: throw new (_a.apply(ClientError, [void 0, _b.sent(), this, res, req, uri]))();
140
- }
141
- });
142
- });
64
+ export async function handleJson(req, res, uri) {
65
+ if (res.ok) {
66
+ return res.json();
67
+ }
68
+ else {
69
+ throw new ClientError(await res.text(), this, res, req, uri);
70
+ }
143
71
  }
144
72
  /**
145
73
  * Checks if a client has access to a resource.
@@ -1,13 +1,13 @@
1
- export var ADMIN_TOKEN = process.env.ADMIN_TOKEN;
2
- export var SUBDOMAIN = process.env.SUBDOMAIN;
3
- export var AUTH_HEADER = ADMIN_TOKEN ? { Authorization: "Bearer ".concat(ADMIN_TOKEN) } : undefined;
4
- export var API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:5000';
5
- export var ENDPOINT = "".concat(API_ENDPOINT, "/api");
6
- export var HEADERS = AUTH_HEADER;
7
- export var NAVIGATION_ROUTE = 'navigation';
8
- export var PATHS_ROUTE = 'paths';
9
- export var HOSTING_LOCATION_ROUTE = 'hosting-location';
10
- export var HIDDEN_PAGES_ROUTE = 'hidden-pages';
11
- export var UNAUTHORIZED = { status: 401 };
12
- export var ACCESS_DENIED = 'Access denied. Not authorized to access protected content.';
13
- export var IS_PASSWORD_PROTECTED = process.env.IS_PASSWORD_PROTECTED === 'true';
1
+ export const ADMIN_TOKEN = process.env.ADMIN_TOKEN;
2
+ export const SUBDOMAIN = process.env.SUBDOMAIN;
3
+ export const AUTH_HEADER = ADMIN_TOKEN ? { Authorization: `Bearer ${ADMIN_TOKEN}` } : undefined;
4
+ export const API_ENDPOINT = process.env.API_ENDPOINT || 'http://localhost:5000';
5
+ export const ENDPOINT = `${API_ENDPOINT}/api`;
6
+ export const HEADERS = AUTH_HEADER;
7
+ export const NAVIGATION_ROUTE = 'navigation';
8
+ export const PATHS_ROUTE = 'paths';
9
+ export const HOSTING_LOCATION_ROUTE = 'hosting-location';
10
+ export const HIDDEN_PAGES_ROUTE = 'hidden-pages';
11
+ export const UNAUTHORIZED = { status: 401 };
12
+ export const ACCESS_DENIED = 'Access denied. Not authorized to access protected content.';
13
+ export const IS_PASSWORD_PROTECTED = process.env.IS_PASSWORD_PROTECTED === 'true';
@@ -7,7 +7,8 @@ export interface ILeavesClient extends IBaseClient {
7
7
  getDeployment(): Promise<DeploymentType>;
8
8
  getNavigation(): Promise<NavGroups>;
9
9
  getSnippets(): Promise<SnippetType[]>;
10
- getPage(path: string, basePath?: string): Promise<CustomerPageType>;
10
+ getPage(path: string, basePath?: string, ...fields: (keyof CustomerPageType)[]): Promise<CustomerPageType>;
11
+ getPages(basePath?: string, ...fields: (keyof CustomerPageType)[]): Promise<CustomerPageType[]>;
11
12
  getHostingLocation(): Promise<string>;
12
13
  getHiddenPages(): Promise<string[]>;
13
14
  getPageProps(path: string, basePath?: string): Promise<GetPagePropsReturnType>;