@periodic/obsidian 1.0.0

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/dist/index.js ADDED
@@ -0,0 +1,424 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ HttpError: () => HttpError,
24
+ HttpStatusCode: () => HttpStatusCode,
25
+ HttpStatusMessage: () => HttpStatusMessage,
26
+ errorHandler: () => errorHandler,
27
+ obsidian: () => obsidian,
28
+ simpleErrorHandler: () => simpleErrorHandler,
29
+ titanium: () => obsidian
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+
33
+ // src/core/status-codes.ts
34
+ var HttpStatusCode = {
35
+ // 1xx Informational
36
+ CONTINUE: 100,
37
+ SWITCHING_PROTOCOLS: 101,
38
+ PROCESSING: 102,
39
+ EARLY_HINTS: 103,
40
+ // 2xx Success
41
+ OK: 200,
42
+ CREATED: 201,
43
+ ACCEPTED: 202,
44
+ NON_AUTHORITATIVE_INFORMATION: 203,
45
+ NO_CONTENT: 204,
46
+ RESET_CONTENT: 205,
47
+ PARTIAL_CONTENT: 206,
48
+ MULTI_STATUS: 207,
49
+ ALREADY_REPORTED: 208,
50
+ IM_USED: 226,
51
+ // 3xx Redirection
52
+ MULTIPLE_CHOICES: 300,
53
+ MOVED_PERMANENTLY: 301,
54
+ FOUND: 302,
55
+ SEE_OTHER: 303,
56
+ NOT_MODIFIED: 304,
57
+ USE_PROXY: 305,
58
+ TEMPORARY_REDIRECT: 307,
59
+ PERMANENT_REDIRECT: 308,
60
+ // 4xx Client Errors
61
+ BAD_REQUEST: 400,
62
+ UNAUTHORIZED: 401,
63
+ PAYMENT_REQUIRED: 402,
64
+ FORBIDDEN: 403,
65
+ NOT_FOUND: 404,
66
+ METHOD_NOT_ALLOWED: 405,
67
+ NOT_ACCEPTABLE: 406,
68
+ PROXY_AUTHENTICATION_REQUIRED: 407,
69
+ REQUEST_TIMEOUT: 408,
70
+ CONFLICT: 409,
71
+ GONE: 410,
72
+ LENGTH_REQUIRED: 411,
73
+ PRECONDITION_FAILED: 412,
74
+ PAYLOAD_TOO_LARGE: 413,
75
+ URI_TOO_LONG: 414,
76
+ UNSUPPORTED_MEDIA_TYPE: 415,
77
+ RANGE_NOT_SATISFIABLE: 416,
78
+ EXPECTATION_FAILED: 417,
79
+ IM_A_TEAPOT: 418,
80
+ MISDIRECTED_REQUEST: 421,
81
+ UNPROCESSABLE_ENTITY: 422,
82
+ LOCKED: 423,
83
+ FAILED_DEPENDENCY: 424,
84
+ TOO_EARLY: 425,
85
+ UPGRADE_REQUIRED: 426,
86
+ PRECONDITION_REQUIRED: 428,
87
+ TOO_MANY_REQUESTS: 429,
88
+ REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
89
+ UNAVAILABLE_FOR_LEGAL_REASONS: 451,
90
+ // 5xx Server Errors
91
+ INTERNAL_SERVER_ERROR: 500,
92
+ NOT_IMPLEMENTED: 501,
93
+ BAD_GATEWAY: 502,
94
+ SERVICE_UNAVAILABLE: 503,
95
+ GATEWAY_TIMEOUT: 504,
96
+ HTTP_VERSION_NOT_SUPPORTED: 505,
97
+ VARIANT_ALSO_NEGOTIATES: 506,
98
+ INSUFFICIENT_STORAGE: 507,
99
+ LOOP_DETECTED: 508,
100
+ NOT_EXTENDED: 510,
101
+ NETWORK_AUTHENTICATION_REQUIRED: 511
102
+ };
103
+ var HttpStatusMessage = {
104
+ // 1xx Informational
105
+ 100: "Continue",
106
+ 101: "Switching Protocols",
107
+ 102: "Processing",
108
+ 103: "Early Hints",
109
+ // 2xx Success
110
+ 200: "OK",
111
+ 201: "Created",
112
+ 202: "Accepted",
113
+ 203: "Non-Authoritative Information",
114
+ 204: "No Content",
115
+ 205: "Reset Content",
116
+ 206: "Partial Content",
117
+ 207: "Multi-Status",
118
+ 208: "Already Reported",
119
+ 226: "IM Used",
120
+ // 3xx Redirection
121
+ 300: "Multiple Choices",
122
+ 301: "Moved Permanently",
123
+ 302: "Found",
124
+ 303: "See Other",
125
+ 304: "Not Modified",
126
+ 305: "Use Proxy",
127
+ 307: "Temporary Redirect",
128
+ 308: "Permanent Redirect",
129
+ // 4xx Client Errors
130
+ 400: "Bad Request",
131
+ 401: "Unauthorized",
132
+ 402: "Payment Required",
133
+ 403: "Forbidden",
134
+ 404: "Not Found",
135
+ 405: "Method Not Allowed",
136
+ 406: "Not Acceptable",
137
+ 407: "Proxy Authentication Required",
138
+ 408: "Request Timeout",
139
+ 409: "Conflict",
140
+ 410: "Gone",
141
+ 411: "Length Required",
142
+ 412: "Precondition Failed",
143
+ 413: "Payload Too Large",
144
+ 414: "URI Too Long",
145
+ 415: "Unsupported Media Type",
146
+ 416: "Range Not Satisfiable",
147
+ 417: "Expectation Failed",
148
+ 418: "I'm a Teapot",
149
+ 421: "Misdirected Request",
150
+ 422: "Unprocessable Entity",
151
+ 423: "Locked",
152
+ 424: "Failed Dependency",
153
+ 425: "Too Early",
154
+ 426: "Upgrade Required",
155
+ 428: "Precondition Required",
156
+ 429: "Too Many Requests",
157
+ 431: "Request Header Fields Too Large",
158
+ 451: "Unavailable For Legal Reasons",
159
+ // 5xx Server Errors
160
+ 500: "Internal Server Error",
161
+ 501: "Not Implemented",
162
+ 502: "Bad Gateway",
163
+ 503: "Service Unavailable",
164
+ 504: "Gateway Timeout",
165
+ 505: "HTTP Version Not Supported",
166
+ 506: "Variant Also Negotiates",
167
+ 507: "Insufficient Storage",
168
+ 508: "Loop Detected",
169
+ 510: "Not Extended",
170
+ 511: "Network Authentication Required"
171
+ };
172
+
173
+ // src/core/http-error.ts
174
+ var HttpError = class _HttpError extends Error {
175
+ /**
176
+ * Creates a new HTTP error
177
+ *
178
+ * @param status - HTTP status code
179
+ * @param message - Human-readable error message
180
+ * @param options - Optional error metadata
181
+ */
182
+ constructor(status, message, options) {
183
+ super(message);
184
+ Object.setPrototypeOf(this, _HttpError.prototype);
185
+ this.name = "HttpError";
186
+ this.status = status;
187
+ this.code = options?.code;
188
+ this.details = options?.details;
189
+ if (Error.captureStackTrace) {
190
+ Error.captureStackTrace(this, this.constructor);
191
+ }
192
+ }
193
+ /**
194
+ * Serializes the error to a plain JSON object
195
+ *
196
+ * @returns JSON representation without stack trace
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const error = new HttpError(404, 'Not found');
201
+ * console.log(error.toJSON());
202
+ * // { status: 404, message: 'Not found' }
203
+ * ```
204
+ */
205
+ toJSON() {
206
+ const json = {
207
+ status: this.status,
208
+ message: this.message
209
+ };
210
+ if (this.code !== void 0) {
211
+ json.code = this.code;
212
+ }
213
+ if (this.details !== void 0) {
214
+ json.details = this.details;
215
+ }
216
+ return json;
217
+ }
218
+ /**
219
+ * Returns the default message for a given HTTP status code
220
+ *
221
+ * @param status - HTTP status code
222
+ * @returns Default status message or 'Unknown Error'
223
+ */
224
+ static getDefaultMessage(status) {
225
+ return HttpStatusMessage[status] || "Unknown Error";
226
+ }
227
+ };
228
+
229
+ // src/adapters/express.ts
230
+ function errorHandler(options = {}) {
231
+ const { includeStack = process.env.NODE_ENV !== "production", logger, transform } = options;
232
+ return (err, req, res, _next) => {
233
+ if (logger) {
234
+ logger(err, req);
235
+ }
236
+ if (err instanceof HttpError) {
237
+ const response = transform ? transform(err) : err.toJSON();
238
+ res.status(err.status).json(response);
239
+ return;
240
+ }
241
+ if (!includeStack) {
242
+ res.status(500).json({
243
+ status: 500,
244
+ message: "Internal Server Error"
245
+ });
246
+ return;
247
+ }
248
+ res.status(500).json({
249
+ status: 500,
250
+ message: err.message,
251
+ stack: err.stack
252
+ });
253
+ };
254
+ }
255
+ function simpleErrorHandler() {
256
+ return (err, _req, res, next) => {
257
+ if (err instanceof HttpError) {
258
+ res.status(err.status).json(err.toJSON());
259
+ return;
260
+ }
261
+ next(err);
262
+ };
263
+ }
264
+
265
+ // src/core/factories.ts
266
+ function createFactory(status) {
267
+ return (message, options) => {
268
+ const errorMessage = message || HttpError.getDefaultMessage(status);
269
+ return new HttpError(status, errorMessage, options);
270
+ };
271
+ }
272
+ var continueError = createFactory(HttpStatusCode.CONTINUE);
273
+ var switchingProtocols = createFactory(HttpStatusCode.SWITCHING_PROTOCOLS);
274
+ var processing = createFactory(HttpStatusCode.PROCESSING);
275
+ var earlyHints = createFactory(HttpStatusCode.EARLY_HINTS);
276
+ var ok = createFactory(HttpStatusCode.OK);
277
+ var created = createFactory(HttpStatusCode.CREATED);
278
+ var accepted = createFactory(HttpStatusCode.ACCEPTED);
279
+ var nonAuthoritativeInformation = createFactory(
280
+ HttpStatusCode.NON_AUTHORITATIVE_INFORMATION
281
+ );
282
+ var noContent = createFactory(HttpStatusCode.NO_CONTENT);
283
+ var resetContent = createFactory(HttpStatusCode.RESET_CONTENT);
284
+ var partialContent = createFactory(HttpStatusCode.PARTIAL_CONTENT);
285
+ var multiStatus = createFactory(HttpStatusCode.MULTI_STATUS);
286
+ var alreadyReported = createFactory(HttpStatusCode.ALREADY_REPORTED);
287
+ var imUsed = createFactory(HttpStatusCode.IM_USED);
288
+ var multipleChoices = createFactory(HttpStatusCode.MULTIPLE_CHOICES);
289
+ var movedPermanently = createFactory(HttpStatusCode.MOVED_PERMANENTLY);
290
+ var found = createFactory(HttpStatusCode.FOUND);
291
+ var seeOther = createFactory(HttpStatusCode.SEE_OTHER);
292
+ var notModified = createFactory(HttpStatusCode.NOT_MODIFIED);
293
+ var useProxy = createFactory(HttpStatusCode.USE_PROXY);
294
+ var temporaryRedirect = createFactory(HttpStatusCode.TEMPORARY_REDIRECT);
295
+ var permanentRedirect = createFactory(HttpStatusCode.PERMANENT_REDIRECT);
296
+ var badRequest = createFactory(HttpStatusCode.BAD_REQUEST);
297
+ var unauthorized = createFactory(HttpStatusCode.UNAUTHORIZED);
298
+ var paymentRequired = createFactory(HttpStatusCode.PAYMENT_REQUIRED);
299
+ var forbidden = createFactory(HttpStatusCode.FORBIDDEN);
300
+ var notFound = createFactory(HttpStatusCode.NOT_FOUND);
301
+ var methodNotAllowed = createFactory(HttpStatusCode.METHOD_NOT_ALLOWED);
302
+ var notAcceptable = createFactory(HttpStatusCode.NOT_ACCEPTABLE);
303
+ var proxyAuthenticationRequired = createFactory(
304
+ HttpStatusCode.PROXY_AUTHENTICATION_REQUIRED
305
+ );
306
+ var requestTimeout = createFactory(HttpStatusCode.REQUEST_TIMEOUT);
307
+ var conflict = createFactory(HttpStatusCode.CONFLICT);
308
+ var gone = createFactory(HttpStatusCode.GONE);
309
+ var lengthRequired = createFactory(HttpStatusCode.LENGTH_REQUIRED);
310
+ var preconditionFailed = createFactory(HttpStatusCode.PRECONDITION_FAILED);
311
+ var payloadTooLarge = createFactory(HttpStatusCode.PAYLOAD_TOO_LARGE);
312
+ var uriTooLong = createFactory(HttpStatusCode.URI_TOO_LONG);
313
+ var unsupportedMediaType = createFactory(HttpStatusCode.UNSUPPORTED_MEDIA_TYPE);
314
+ var rangeNotSatisfiable = createFactory(HttpStatusCode.RANGE_NOT_SATISFIABLE);
315
+ var expectationFailed = createFactory(HttpStatusCode.EXPECTATION_FAILED);
316
+ var imATeapot = createFactory(HttpStatusCode.IM_A_TEAPOT);
317
+ var misdirectedRequest = createFactory(HttpStatusCode.MISDIRECTED_REQUEST);
318
+ var unprocessableEntity = createFactory(HttpStatusCode.UNPROCESSABLE_ENTITY);
319
+ var locked = createFactory(HttpStatusCode.LOCKED);
320
+ var failedDependency = createFactory(HttpStatusCode.FAILED_DEPENDENCY);
321
+ var tooEarly = createFactory(HttpStatusCode.TOO_EARLY);
322
+ var upgradeRequired = createFactory(HttpStatusCode.UPGRADE_REQUIRED);
323
+ var preconditionRequired = createFactory(HttpStatusCode.PRECONDITION_REQUIRED);
324
+ var tooManyRequests = createFactory(HttpStatusCode.TOO_MANY_REQUESTS);
325
+ var requestHeaderFieldsTooLarge = createFactory(
326
+ HttpStatusCode.REQUEST_HEADER_FIELDS_TOO_LARGE
327
+ );
328
+ var unavailableForLegalReasons = createFactory(
329
+ HttpStatusCode.UNAVAILABLE_FOR_LEGAL_REASONS
330
+ );
331
+ var internalServerError = createFactory(HttpStatusCode.INTERNAL_SERVER_ERROR);
332
+ var notImplemented = createFactory(HttpStatusCode.NOT_IMPLEMENTED);
333
+ var badGateway = createFactory(HttpStatusCode.BAD_GATEWAY);
334
+ var serviceUnavailable = createFactory(HttpStatusCode.SERVICE_UNAVAILABLE);
335
+ var gatewayTimeout = createFactory(HttpStatusCode.GATEWAY_TIMEOUT);
336
+ var httpVersionNotSupported = createFactory(HttpStatusCode.HTTP_VERSION_NOT_SUPPORTED);
337
+ var variantAlsoNegotiates = createFactory(HttpStatusCode.VARIANT_ALSO_NEGOTIATES);
338
+ var insufficientStorage = createFactory(HttpStatusCode.INSUFFICIENT_STORAGE);
339
+ var loopDetected = createFactory(HttpStatusCode.LOOP_DETECTED);
340
+ var notExtended = createFactory(HttpStatusCode.NOT_EXTENDED);
341
+ var networkAuthenticationRequired = createFactory(
342
+ HttpStatusCode.NETWORK_AUTHENTICATION_REQUIRED
343
+ );
344
+
345
+ // src/index.ts
346
+ var obsidian = {
347
+ // 1xx Informational
348
+ continue: continueError,
349
+ switchingProtocols,
350
+ processing,
351
+ earlyHints,
352
+ // 2xx Success
353
+ ok,
354
+ created,
355
+ accepted,
356
+ nonAuthoritativeInformation,
357
+ noContent,
358
+ resetContent,
359
+ partialContent,
360
+ multiStatus,
361
+ alreadyReported,
362
+ imUsed,
363
+ // 3xx Redirection
364
+ multipleChoices,
365
+ movedPermanently,
366
+ found,
367
+ seeOther,
368
+ notModified,
369
+ useProxy,
370
+ temporaryRedirect,
371
+ permanentRedirect,
372
+ // 4xx Client Errors
373
+ badRequest,
374
+ unauthorized,
375
+ paymentRequired,
376
+ forbidden,
377
+ notFound,
378
+ methodNotAllowed,
379
+ notAcceptable,
380
+ proxyAuthenticationRequired,
381
+ requestTimeout,
382
+ conflict,
383
+ gone,
384
+ lengthRequired,
385
+ preconditionFailed,
386
+ payloadTooLarge,
387
+ uriTooLong,
388
+ unsupportedMediaType,
389
+ rangeNotSatisfiable,
390
+ expectationFailed,
391
+ imATeapot,
392
+ misdirectedRequest,
393
+ unprocessableEntity,
394
+ locked,
395
+ failedDependency,
396
+ tooEarly,
397
+ upgradeRequired,
398
+ preconditionRequired,
399
+ tooManyRequests,
400
+ requestHeaderFieldsTooLarge,
401
+ unavailableForLegalReasons,
402
+ // 5xx Server Errors
403
+ internalServerError,
404
+ badGateway,
405
+ serviceUnavailable,
406
+ gatewayTimeout,
407
+ notImplemented,
408
+ httpVersionNotSupported,
409
+ variantAlsoNegotiates,
410
+ insufficientStorage,
411
+ loopDetected,
412
+ notExtended,
413
+ networkAuthenticationRequired
414
+ };
415
+ // Annotate the CommonJS export names for ESM import in node:
416
+ 0 && (module.exports = {
417
+ HttpError,
418
+ HttpStatusCode,
419
+ HttpStatusMessage,
420
+ errorHandler,
421
+ obsidian,
422
+ simpleErrorHandler,
423
+ titanium
424
+ });