@auldrant/api 0.0.4 → 0.1.1

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,19 +1,76 @@
1
1
  # @auldrant/api
2
2
 
3
- @auldrant/api is a client-side library intended to simplify working with Web APIs so you can focus on your core application logic. It also provides some content useful for server-side applications as well. We recommend using [Bun](https://bun.sh) to work with @auldrant/api. To install
3
+ @auldrant/api is a client-side library that makes REST API calls simple and correct. It wraps the [Fetch API][fetch] with ergonomic method helpers, automatic JSON serialization, and a discriminated union response type so you always know whether a request succeeded.
4
+
5
+ We recommend using [Bun](https://bun.sh) to work with @auldrant/api.
4
6
 
5
7
  ```bash
6
8
  bun install
7
9
  ```
8
10
 
9
- ## Static Content
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { api } from '@auldrant/api';
15
+
16
+ const result = await api.get<User[]>('/api/users');
17
+ if (result.ok) {
18
+ console.log(result.data); // User[]
19
+ } else {
20
+ console.error(result.status); // HTTP status code, or 0 for network errors
21
+ }
22
+ ```
23
+
24
+ ## API
25
+
26
+ ### Method helpers
27
+
28
+ All helpers return `Promise<ApiResponse<T>>`.
29
+
30
+ | Method | Signature |
31
+ |--------|-----------|
32
+ | `api.get` | `(url, options?)` |
33
+ | `api.post` | `(url, body?, options?)` |
34
+ | `api.put` | `(url, body?, options?)` |
35
+ | `api.patch` | `(url, body?, options?)` |
36
+ | `api.delete` | `(url, options?)` |
37
+ | `api.head` | `(url, options?)` |
38
+ | `api.options` | `(url, options?)` |
39
+
40
+ Plain objects passed as `body` are automatically serialized to JSON.
10
41
 
11
- @auldrant/api is designed to provide simple, reusable code for common work with APIs. All static content can be found in [static.ts][static]. This includes common content like HTTP methods and status codes, as well as many types for the most common HTTP headers including MIME types and encodings, and some helper typings for working with requests and responses.
42
+ ### ApiResponse
43
+
44
+ ```ts
45
+ type ApiResponse<T> =
46
+ | { ok: true; data: T; status: number }
47
+ | { ok: false; data: null; status: number };
48
+ ```
49
+
50
+ Use `ok` to narrow the type. Status `0` means a network error or aborted request.
51
+
52
+ ### Options
53
+
54
+ ```ts
55
+ interface RequestOptions {
56
+ accept?: MimeType; // Default: MimeType.JSON
57
+ headers?: HeadersInit;
58
+ signal?: AbortSignal;
59
+ }
60
+
61
+ interface RequestBodyOptions extends RequestOptions {
62
+ contentType?: MimeType; // Default: MimeType.JSON
63
+ compression?: CompressionMethod; // gzip or deflate
64
+ }
65
+ ```
66
+
67
+ ## Static Content
12
68
 
13
- ## Methods
69
+ All enums and types are re-exported from the package root:
14
70
 
15
- @auldrant/api's main feature is [apiCall], which aims to provide a simpler interface for working with [fetch] that handles some of the technical operations automatically, behind the scenes. For example, it will automatically perform supported compression on content upon request and set the appropriate `Content-Encoding` header.
71
+ - `HttpMethod` GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
72
+ - `HttpStatus` — common HTTP status codes
73
+ - `MimeType` — common MIME type strings
74
+ - `CompressionMethod` — gzip, deflate
16
75
 
17
- [static]: ./src/static.ts
18
- [apiCall]: ./src/apiCall.ts
19
76
  [fetch]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
package/dist/api.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { ApiResponse, RequestBodyOptions, RequestOptions } from './static.ts';
2
+ /**
3
+ * RESTful API client with method helpers.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const result = await api.get<User[]>('/api/users');
8
+ * if (result.ok) {
9
+ * console.log(result.data); // User[]
10
+ * }
11
+ * ```
12
+ */
13
+ export declare const api: {
14
+ /**
15
+ * Sends a GET request.
16
+ * @param url - Request URL
17
+ * @param options - Request options (accept, headers, signal)
18
+ */
19
+ get<T>(url: string | URL, options?: RequestOptions): Promise<ApiResponse<T>>;
20
+ /**
21
+ * Sends a POST request.
22
+ * @param url - Request URL
23
+ * @param body - Request body (auto-serialized to JSON if plain object)
24
+ * @param options - Request options (contentType, accept, headers, signal, compression)
25
+ */
26
+ post<T>(url: string | URL, body?: BodyInit | object, options?: RequestBodyOptions): Promise<ApiResponse<T>>;
27
+ /**
28
+ * Sends a PUT request.
29
+ * @param url - Request URL
30
+ * @param body - Request body (auto-serialized to JSON if plain object)
31
+ * @param options - Request options (contentType, accept, headers, signal, compression)
32
+ */
33
+ put<T>(url: string | URL, body?: BodyInit | object, options?: RequestBodyOptions): Promise<ApiResponse<T>>;
34
+ /**
35
+ * Sends a PATCH request.
36
+ * @param url - Request URL
37
+ * @param body - Request body (auto-serialized to JSON if plain object)
38
+ * @param options - Request options (contentType, accept, headers, signal, compression)
39
+ */
40
+ patch<T>(url: string | URL, body?: BodyInit | object, options?: RequestBodyOptions): Promise<ApiResponse<T>>;
41
+ /**
42
+ * Sends a DELETE request.
43
+ * @param url - Request URL
44
+ * @param options - Request options (accept, headers, signal)
45
+ */
46
+ delete<T>(url: string | URL, options?: RequestOptions): Promise<ApiResponse<T>>;
47
+ /**
48
+ * Sends a HEAD request. Returns status and headers only (no body).
49
+ * @param url - Request URL
50
+ * @param options - Request options (headers, signal)
51
+ */
52
+ head(url: string | URL, options?: RequestOptions): Promise<ApiResponse<null>>;
53
+ /**
54
+ * Sends an OPTIONS request. Returns allowed methods and CORS info.
55
+ * @param url - Request URL
56
+ * @param options - Request options (accept, headers, signal)
57
+ */
58
+ options<T>(url: string | URL, options?: RequestOptions): Promise<ApiResponse<T>>;
59
+ };
@@ -0,0 +1,134 @@
1
+ var l = /* @__PURE__ */ ((E) => (E[E.OK = 200] = "OK", E[E.CREATED = 201] = "CREATED", E[E.ACCEPTED = 202] = "ACCEPTED", E[E.NO_CONTENT = 204] = "NO_CONTENT", E[E.PARTIAL_CONTENT = 206] = "PARTIAL_CONTENT", E[E.MOVED_PERMANENTLY = 301] = "MOVED_PERMANENTLY", E[E.FOUND = 302] = "FOUND", E[E.SEE_OTHER = 303] = "SEE_OTHER", E[E.NOT_MODIFIED = 304] = "NOT_MODIFIED", E[E.TEMPORARY_REDIRECT = 307] = "TEMPORARY_REDIRECT", E[E.PERMANENT_REDIRECT = 308] = "PERMANENT_REDIRECT", E[E.BAD_REQUEST = 400] = "BAD_REQUEST", E[E.UNAUTHORIZED = 401] = "UNAUTHORIZED", E[E.PAYMENT_REQUIRED = 402] = "PAYMENT_REQUIRED", E[E.FORBIDDEN = 403] = "FORBIDDEN", E[E.NOT_FOUND = 404] = "NOT_FOUND", E[E.METHOD_NOT_ALLOWED = 405] = "METHOD_NOT_ALLOWED", E[E.NOT_ACCEPTABLE = 406] = "NOT_ACCEPTABLE", E[E.PROXY_AUTHENTICATION_REQUIRED = 407] = "PROXY_AUTHENTICATION_REQUIRED", E[E.REQUEST_TIMEOUT = 408] = "REQUEST_TIMEOUT", E[E.CONFLICT = 409] = "CONFLICT", E[E.GONE = 410] = "GONE", E[E.LENGTH_REQUIRED = 411] = "LENGTH_REQUIRED", E[E.PRECONDITION_FAILED = 412] = "PRECONDITION_FAILED", E[E.PAYLOAD_TOO_LARGE = 413] = "PAYLOAD_TOO_LARGE", E[E.URI_TOO_LONG = 414] = "URI_TOO_LONG", E[E.UNSUPPORTED_MEDIA_TYPE = 415] = "UNSUPPORTED_MEDIA_TYPE", E[E.RANGE_NOT_SATISFIABLE = 416] = "RANGE_NOT_SATISFIABLE", E[E.EXPECTATION_FAILED = 417] = "EXPECTATION_FAILED", E[E.IM_A_TEAPOT = 418] = "IM_A_TEAPOT", E[E.MISDIRECTED_REQUEST = 421] = "MISDIRECTED_REQUEST", E[E.UNPROCESSABLE_ENTITY = 422] = "UNPROCESSABLE_ENTITY", E[E.LOCKED = 423] = "LOCKED", E[E.FAILED_DEPENDENCY = 424] = "FAILED_DEPENDENCY", E[E.TOO_EARLY = 425] = "TOO_EARLY", E[E.UPGRADE_REQUIRED = 426] = "UPGRADE_REQUIRED", E[E.PRECONDITION_REQUIRED = 428] = "PRECONDITION_REQUIRED", E[E.TOO_MANY_REQUESTS = 429] = "TOO_MANY_REQUESTS", E[E.REQUEST_HEADER_FIELDS_TOO_LARGE = 431] = "REQUEST_HEADER_FIELDS_TOO_LARGE", E[E.UNAVAILABLE_FOR_LEGAL_REASONS = 451] = "UNAVAILABLE_FOR_LEGAL_REASONS", E[E.INTERNAL_SERVER_ERROR = 500] = "INTERNAL_SERVER_ERROR", E[E.NOT_IMPLEMENTED = 501] = "NOT_IMPLEMENTED", E[E.BAD_GATEWAY = 502] = "BAD_GATEWAY", E[E.SERVICE_UNAVAILABLE = 503] = "SERVICE_UNAVAILABLE", E[E.GATEWAY_TIMEOUT = 504] = "GATEWAY_TIMEOUT", E[E.HTTP_VERSION_NOT_SUPPORTED = 505] = "HTTP_VERSION_NOT_SUPPORTED", E[E.VARIANT_ALSO_NEGOTIATES = 506] = "VARIANT_ALSO_NEGOTIATES", E[E.INSUFFICIENT_STORAGE = 507] = "INSUFFICIENT_STORAGE", E[E.LOOP_DETECTED = 508] = "LOOP_DETECTED", E[E.NOT_EXTENDED = 510] = "NOT_EXTENDED", E[E.NETWORK_AUTHENTICATION_REQUIRED = 511] = "NETWORK_AUTHENTICATION_REQUIRED", E))(l || {}), R = /* @__PURE__ */ ((E) => (E.HTML = "text/html", E.PLAIN = "text/plain", E.CSV = "text/csv", E.CSS = "text/css", E.JAVASCRIPT = "text/javascript", E.JSON = "application/json", E.XML = "application/xml", E.PDF = "application/pdf", E.ZIP = "application/zip", E.GZIP = "application/gzip", E.OCTET_STREAM = "application/octet-stream", E.FORM_DATA = "multipart/form-data", E.URL_ENCODED = "application/x-www-form-urlencoded", E.JPEG = "image/jpeg", E.PNG = "image/png", E.GIF = "image/gif", E.SVG = "image/svg+xml", E.WEBP = "image/webp", E.ICO = "image/x-icon", E.MP4 = "video/mp4", E.WEBM = "video/webm", E.MP3 = "audio/mpeg", E.OGG = "audio/ogg", E.WAV = "audio/wav", E.TAR = "application/x-tar", E))(R || {}), A = /* @__PURE__ */ ((E) => (E.GET = "GET", E.POST = "POST", E.PUT = "PUT", E.DELETE = "DELETE", E.PATCH = "PATCH", E.OPTIONS = "OPTIONS", E.HEAD = "HEAD", E))(A || {}), c = /* @__PURE__ */ ((E) => (E.GZIP = "gzip", E.DEFLATE = "deflate", E))(c || {});
2
+ const o = 1024;
3
+ function f(E, T) {
4
+ return T == null || E instanceof FormData || E instanceof URLSearchParams ? E : E instanceof ReadableStream ? E.pipeThrough(new CompressionStream(T)) : (typeof E == "string" ? E.length : E instanceof Blob ? E.size : E.byteLength) < o ? E : new Blob([E]).stream().pipeThrough(new CompressionStream(T));
5
+ }
6
+ function G(E, T, O, n, r) {
7
+ const _ = new Headers(n);
8
+ _.has("Accept") || _.set("Accept", T);
9
+ const D = r instanceof FormData || r instanceof URLSearchParams;
10
+ return E != null && !D && !_.has("Content-Type") && _.set("Content-Type", E), O != null && !_.has("Content-Encoding") && _.set("Content-Encoding", O), _;
11
+ }
12
+ async function a(E, T) {
13
+ switch (T) {
14
+ case R.JSON:
15
+ return await E.json();
16
+ case R.PLAIN:
17
+ case R.HTML:
18
+ case R.CSV:
19
+ case R.XML:
20
+ case R.CSS:
21
+ case R.JAVASCRIPT:
22
+ return await E.text();
23
+ default:
24
+ return await E.blob();
25
+ }
26
+ }
27
+ async function N(E, T, O, n = {}) {
28
+ const {
29
+ accept: r = R.JSON,
30
+ contentType: _ = R.JSON,
31
+ compression: D,
32
+ headers: C,
33
+ signal: i
34
+ } = n;
35
+ try {
36
+ const L = O != null && T !== A.GET && T !== A.HEAD ? f(O, D) : null, U = G(
37
+ O != null ? _ : void 0,
38
+ r,
39
+ O != null ? D : void 0,
40
+ C,
41
+ O
42
+ ), e = {
43
+ method: T,
44
+ body: L,
45
+ headers: U
46
+ };
47
+ i != null && (e.signal = i);
48
+ const I = await fetch(E, e);
49
+ return I.ok ? T === A.HEAD || I.status === 204 ? { ok: !0, data: null, status: I.status } : { ok: !0, data: await a(I, r), status: I.status } : { ok: !1, data: null, status: I.status };
50
+ } catch {
51
+ return { ok: !1, data: null, status: 0 };
52
+ }
53
+ }
54
+ function P(E) {
55
+ return E instanceof FormData || E instanceof URLSearchParams || E instanceof Blob || E instanceof ArrayBuffer || E instanceof ReadableStream || typeof E == "string" ? E : JSON.stringify(E);
56
+ }
57
+ const g = {
58
+ /**
59
+ * Sends a GET request.
60
+ * @param url - Request URL
61
+ * @param options - Request options (accept, headers, signal)
62
+ */
63
+ get(E, T) {
64
+ return N(E, A.GET, void 0, T);
65
+ },
66
+ /**
67
+ * Sends a POST request.
68
+ * @param url - Request URL
69
+ * @param body - Request body (auto-serialized to JSON if plain object)
70
+ * @param options - Request options (contentType, accept, headers, signal, compression)
71
+ */
72
+ post(E, T, O) {
73
+ return N(
74
+ E,
75
+ A.POST,
76
+ T != null ? P(T) : void 0,
77
+ O
78
+ );
79
+ },
80
+ /**
81
+ * Sends a PUT request.
82
+ * @param url - Request URL
83
+ * @param body - Request body (auto-serialized to JSON if plain object)
84
+ * @param options - Request options (contentType, accept, headers, signal, compression)
85
+ */
86
+ put(E, T, O) {
87
+ return N(E, A.PUT, T != null ? P(T) : void 0, O);
88
+ },
89
+ /**
90
+ * Sends a PATCH request.
91
+ * @param url - Request URL
92
+ * @param body - Request body (auto-serialized to JSON if plain object)
93
+ * @param options - Request options (contentType, accept, headers, signal, compression)
94
+ */
95
+ patch(E, T, O) {
96
+ return N(
97
+ E,
98
+ A.PATCH,
99
+ T != null ? P(T) : void 0,
100
+ O
101
+ );
102
+ },
103
+ /**
104
+ * Sends a DELETE request.
105
+ * @param url - Request URL
106
+ * @param options - Request options (accept, headers, signal)
107
+ */
108
+ delete(E, T) {
109
+ return N(E, A.DELETE, void 0, T);
110
+ },
111
+ /**
112
+ * Sends a HEAD request. Returns status and headers only (no body).
113
+ * @param url - Request URL
114
+ * @param options - Request options (headers, signal)
115
+ */
116
+ head(E, T) {
117
+ return N(E, A.HEAD, void 0, T);
118
+ },
119
+ /**
120
+ * Sends an OPTIONS request. Returns allowed methods and CORS info.
121
+ * @param url - Request URL
122
+ * @param options - Request options (accept, headers, signal)
123
+ */
124
+ options(E, T) {
125
+ return N(E, A.OPTIONS, void 0, T);
126
+ }
127
+ };
128
+ export {
129
+ c as CompressionMethod,
130
+ A as HttpMethod,
131
+ l as HttpStatus,
132
+ R as MimeType,
133
+ g as api
134
+ };
package/dist/index.d.ts CHANGED
@@ -1,110 +1,2 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export declare const enum HttpStatus {
4
- OK = 200,
5
- CREATED = 201,
6
- ACCEPTED = 202,
7
- NO_CONTENT = 204,
8
- PARTIAL_CONTENT = 206,
9
- MOVED_PERMANENTLY = 301,
10
- FOUND = 302,
11
- SEE_OTHER = 303,
12
- NOT_MODIFIED = 304,
13
- TEMPORARY_REDIRECT = 307,
14
- PERMANENT_REDIRECT = 308,
15
- BAD_REQUEST = 400,
16
- UNAUTHORIZED = 401,
17
- PAYMENT_REQUIRED = 402,
18
- FORBIDDEN = 403,
19
- NOT_FOUND = 404,
20
- METHOD_NOT_ALLOWED = 405,
21
- NOT_ACCEPTABLE = 406,
22
- PROXY_AUTHENTICATION_REQUIRED = 407,
23
- REQUEST_TIMEOUT = 408,
24
- CONFLICT = 409,
25
- GONE = 410,
26
- LENGTH_REQUIRED = 411,
27
- PRECONDITION_FAILED = 412,
28
- PAYLOAD_TOO_LARGE = 413,
29
- URI_TOO_LONG = 414,
30
- UNSUPPORTED_MEDIA_TYPE = 415,
31
- RANGE_NOT_SATISFIABLE = 416,
32
- EXPECTATION_FAILED = 417,
33
- IM_A_TEAPOT = 418,
34
- MISDIRECTED_REQUEST = 421,
35
- UNPROCESSABLE_ENTITY = 422,
36
- LOCKED = 423,
37
- FAILED_DEPENDENCY = 424,
38
- TOO_EARLY = 425,
39
- UPGRADE_REQUIRED = 426,
40
- PRECONDITION_REQUIRED = 428,
41
- TOO_MANY_REQUESTS = 429,
42
- REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
43
- UNAVAILABLE_FOR_LEGAL_REASONS = 451,
44
- INTERNAL_SERVER_ERROR = 500,
45
- NOT_IMPLEMENTED = 501,
46
- BAD_GATEWAY = 502,
47
- SERVICE_UNAVAILABLE = 503,
48
- GATEWAY_TIMEOUT = 504,
49
- HTTP_VERSION_NOT_SUPPORTED = 505,
50
- VARIANT_ALSO_NEGOTIATES = 506,
51
- INSUFFICIENT_STORAGE = 507,
52
- LOOP_DETECTED = 508,
53
- NOT_EXTENDED = 510,
54
- NETWORK_AUTHENTICATION_REQUIRED = 511
55
- }
56
- declare const enum MimeType$1 {
57
- HTML = "text/html",
58
- PLAIN = "text/plain",
59
- CSV = "text/csv",
60
- CSS = "text/css",
61
- JAVASCRIPT = "text/javascript",
62
- JSON = "application/json",
63
- XML = "application/xml",
64
- PDF = "application/pdf",
65
- ZIP = "application/zip",
66
- GZIP = "application/gzip",
67
- OCTET_STREAM = "application/octet-stream",
68
- JPEG = "image/jpeg",
69
- PNG = "image/png",
70
- GIF = "image/gif",
71
- SVG = "image/svg+xml",
72
- WEBP = "image/webp",
73
- MP4 = "video/mp4",
74
- WEBM = "video/webm",
75
- MP3 = "audio/mpeg",
76
- OGG = "audio/ogg",
77
- WAV = "audio/wav",
78
- ICO = "image/x-icon",
79
- TAR = "application/x-tar"
80
- }
81
- export declare const enum HttpMethod {
82
- GET = "GET",
83
- POST = "POST",
84
- PUT = "PUT",
85
- DELETE = "DELETE",
86
- PATCH = "PATCH",
87
- OPTIONS = "OPTIONS",
88
- HEAD = "HEAD"
89
- }
90
- export declare const enum CompressionMethod {
91
- GZIP = "gzip",
92
- DEFLATE = "deflate"
93
- }
94
- export interface IRequestArgs {
95
- body?: BodyInit;
96
- contentType?: MimeType$1;
97
- accept?: MimeType$1;
98
- compression?: CompressionMethod;
99
- }
100
- export declare const apiCall: <T>(url: string | URL, method: HttpMethod, requestArgs: IRequestArgs, controller: AbortController) => Promise<{
101
- data: T | null;
102
- status: HttpStatus;
103
- }>;
104
- export declare function logRequest(req: Request): void;
105
-
106
- export {
107
- MimeType$1 as MimeType,
108
- };
109
-
110
- export {};
1
+ export { api } from './api.ts';
2
+ export { type ApiResponse, CompressionMethod, HttpMethod, HttpStatus, MimeType, type RequestBodyOptions, type RequestOptions, } from './static.ts';
@@ -0,0 +1,122 @@
1
+ export declare enum HttpStatus {
2
+ OK = 200,
3
+ CREATED = 201,
4
+ ACCEPTED = 202,
5
+ NO_CONTENT = 204,
6
+ PARTIAL_CONTENT = 206,
7
+ MOVED_PERMANENTLY = 301,
8
+ FOUND = 302,
9
+ SEE_OTHER = 303,
10
+ NOT_MODIFIED = 304,
11
+ TEMPORARY_REDIRECT = 307,
12
+ PERMANENT_REDIRECT = 308,
13
+ BAD_REQUEST = 400,
14
+ UNAUTHORIZED = 401,
15
+ PAYMENT_REQUIRED = 402,
16
+ FORBIDDEN = 403,
17
+ NOT_FOUND = 404,
18
+ METHOD_NOT_ALLOWED = 405,
19
+ NOT_ACCEPTABLE = 406,
20
+ PROXY_AUTHENTICATION_REQUIRED = 407,
21
+ REQUEST_TIMEOUT = 408,
22
+ CONFLICT = 409,
23
+ GONE = 410,
24
+ LENGTH_REQUIRED = 411,
25
+ PRECONDITION_FAILED = 412,
26
+ PAYLOAD_TOO_LARGE = 413,
27
+ URI_TOO_LONG = 414,
28
+ UNSUPPORTED_MEDIA_TYPE = 415,
29
+ RANGE_NOT_SATISFIABLE = 416,
30
+ EXPECTATION_FAILED = 417,
31
+ IM_A_TEAPOT = 418,
32
+ MISDIRECTED_REQUEST = 421,
33
+ UNPROCESSABLE_ENTITY = 422,
34
+ LOCKED = 423,
35
+ FAILED_DEPENDENCY = 424,
36
+ TOO_EARLY = 425,
37
+ UPGRADE_REQUIRED = 426,
38
+ PRECONDITION_REQUIRED = 428,
39
+ TOO_MANY_REQUESTS = 429,
40
+ REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
41
+ UNAVAILABLE_FOR_LEGAL_REASONS = 451,
42
+ INTERNAL_SERVER_ERROR = 500,
43
+ NOT_IMPLEMENTED = 501,
44
+ BAD_GATEWAY = 502,
45
+ SERVICE_UNAVAILABLE = 503,
46
+ GATEWAY_TIMEOUT = 504,
47
+ HTTP_VERSION_NOT_SUPPORTED = 505,
48
+ VARIANT_ALSO_NEGOTIATES = 506,
49
+ INSUFFICIENT_STORAGE = 507,
50
+ LOOP_DETECTED = 508,
51
+ NOT_EXTENDED = 510,
52
+ NETWORK_AUTHENTICATION_REQUIRED = 511
53
+ }
54
+ export declare enum MimeType {
55
+ HTML = "text/html",
56
+ PLAIN = "text/plain",
57
+ CSV = "text/csv",
58
+ CSS = "text/css",
59
+ JAVASCRIPT = "text/javascript",
60
+ JSON = "application/json",
61
+ XML = "application/xml",
62
+ PDF = "application/pdf",
63
+ ZIP = "application/zip",
64
+ GZIP = "application/gzip",
65
+ OCTET_STREAM = "application/octet-stream",
66
+ FORM_DATA = "multipart/form-data",
67
+ URL_ENCODED = "application/x-www-form-urlencoded",
68
+ JPEG = "image/jpeg",
69
+ PNG = "image/png",
70
+ GIF = "image/gif",
71
+ SVG = "image/svg+xml",
72
+ WEBP = "image/webp",
73
+ ICO = "image/x-icon",
74
+ MP4 = "video/mp4",
75
+ WEBM = "video/webm",
76
+ MP3 = "audio/mpeg",
77
+ OGG = "audio/ogg",
78
+ WAV = "audio/wav",
79
+ TAR = "application/x-tar"
80
+ }
81
+ export declare enum HttpMethod {
82
+ GET = "GET",
83
+ POST = "POST",
84
+ PUT = "PUT",
85
+ DELETE = "DELETE",
86
+ PATCH = "PATCH",
87
+ OPTIONS = "OPTIONS",
88
+ HEAD = "HEAD"
89
+ }
90
+ export declare enum CompressionMethod {
91
+ GZIP = "gzip",
92
+ DEFLATE = "deflate"
93
+ }
94
+ /** Options for requests that do not carry a body (GET, HEAD, OPTIONS, DELETE). */
95
+ export interface RequestOptions {
96
+ /** Expected response MIME type. Defaults to `MimeType.JSON`. */
97
+ accept?: MimeType;
98
+ /** Additional headers to include in the request. */
99
+ headers?: HeadersInit;
100
+ /** AbortSignal to cancel the request. */
101
+ signal?: AbortSignal;
102
+ }
103
+ /** Options for requests that carry a body (POST, PUT, PATCH). */
104
+ export interface RequestBodyOptions extends RequestOptions {
105
+ /** Request body content type. Defaults to `MimeType.JSON`. */
106
+ contentType?: MimeType;
107
+ /** Compress the request body before sending. */
108
+ compression?: CompressionMethod;
109
+ }
110
+ /**
111
+ * Discriminated union for API responses.
112
+ * Use `ok` to narrow: if `ok` is true, `data` is `T`; otherwise `data` is `null`.
113
+ */
114
+ export type ApiResponse<T> = {
115
+ ok: true;
116
+ data: T | null;
117
+ status: number;
118
+ } | {
119
+ ok: false;
120
+ data: null;
121
+ status: number;
122
+ };
package/package.json CHANGED
@@ -1,30 +1,24 @@
1
1
  {
2
2
  "name": "@auldrant/api",
3
- "version": "0.0.4",
3
+ "version": "0.1.1",
4
+ "description": "Simple library for working with APIs",
4
5
  "author": "Colonel Jade <colonel.jade@proton.me> (https://github.com/coloneljade/)",
5
- "repository": "github:coloneljade/auldrant-api",
6
- "homepage": "https://github.com/coloneljade/auldrant-api#readme",
7
6
  "license": "MIT",
8
- "type": "module",
9
- "module": "dist/index.js",
10
- "devDependencies": {
11
- "@types/bun": "^1.1.14",
12
- "bun-plugin-dts": "^0.3.0",
13
- "fs": "^0.0.1-security",
14
- "oxc-transform": "^0.43.0",
15
- "path": "^0.12.7",
16
- "typescript": "^5.7.2",
17
- "yargs": "^17.7.2"
18
- },
19
- "exports": {
20
- ".": "./dist/index.js"
21
- },
22
- "types": "./dist/index.d.ts",
7
+ "homepage": "https://github.com/coloneljade/auldrant-api#readme",
8
+ "repository": "github:coloneljade/auldrant-api",
23
9
  "bugs": {
24
10
  "url": "https://github.com/coloneljade/auldrant-api/issues",
25
11
  "email": "colonel.jade@proton.me"
26
12
  },
27
- "description": "Simple library for working with APIs",
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/auldrant-api.js"
18
+ }
19
+ },
20
+ "module": "dist/auldrant-api.js",
21
+ "types": "./dist/index.d.ts",
28
22
  "files": [
29
23
  "dist/**/*.js",
30
24
  "dist/**/*.d.ts",
@@ -33,9 +27,23 @@
33
27
  "publishConfig": {
34
28
  "access": "public"
35
29
  },
30
+ "devDependencies": {
31
+ "@biomejs/biome": "^2.4.4",
32
+ "@types/bun": "^1.1.14",
33
+ "lefthook": "^2.1.1",
34
+ "typescript": "^5.7.2",
35
+ "vite": "^7.3.1",
36
+ "vite-plugin-dts": "^4.5.4"
37
+ },
36
38
  "scripts": {
37
- "build": "bun clean && bun ./build.ts",
38
- "clean": "bun ./clean.ts --dir dist",
39
- "publish": "bun test && bun publish --frozen-lockfile"
39
+ "build": "vite build",
40
+ "check": "biome check .",
41
+ "check:fix": "biome check --fix .",
42
+ "format": "biome format --fix .",
43
+ "lint": "biome lint .",
44
+ "test": "bun test",
45
+ "test:watch": "bun test --watch",
46
+ "typecheck": "tsc --noEmit",
47
+ "prepare": "test -n \"$CI\" || lefthook install"
40
48
  }
41
49
  }
package/dist/index.js DELETED
@@ -1 +0,0 @@
1
- var D;((E)=>{E[E.OK=200]="OK";E[E.CREATED=201]="CREATED";E[E.ACCEPTED=202]="ACCEPTED";E[E.NO_CONTENT=204]="NO_CONTENT";E[E.PARTIAL_CONTENT=206]="PARTIAL_CONTENT";E[E.MOVED_PERMANENTLY=301]="MOVED_PERMANENTLY";E[E.FOUND=302]="FOUND";E[E.SEE_OTHER=303]="SEE_OTHER";E[E.NOT_MODIFIED=304]="NOT_MODIFIED";E[E.TEMPORARY_REDIRECT=307]="TEMPORARY_REDIRECT";E[E.PERMANENT_REDIRECT=308]="PERMANENT_REDIRECT";E[E.BAD_REQUEST=400]="BAD_REQUEST";E[E.UNAUTHORIZED=401]="UNAUTHORIZED";E[E.PAYMENT_REQUIRED=402]="PAYMENT_REQUIRED";E[E.FORBIDDEN=403]="FORBIDDEN";E[E.NOT_FOUND=404]="NOT_FOUND";E[E.METHOD_NOT_ALLOWED=405]="METHOD_NOT_ALLOWED";E[E.NOT_ACCEPTABLE=406]="NOT_ACCEPTABLE";E[E.PROXY_AUTHENTICATION_REQUIRED=407]="PROXY_AUTHENTICATION_REQUIRED";E[E.REQUEST_TIMEOUT=408]="REQUEST_TIMEOUT";E[E.CONFLICT=409]="CONFLICT";E[E.GONE=410]="GONE";E[E.LENGTH_REQUIRED=411]="LENGTH_REQUIRED";E[E.PRECONDITION_FAILED=412]="PRECONDITION_FAILED";E[E.PAYLOAD_TOO_LARGE=413]="PAYLOAD_TOO_LARGE";E[E.URI_TOO_LONG=414]="URI_TOO_LONG";E[E.UNSUPPORTED_MEDIA_TYPE=415]="UNSUPPORTED_MEDIA_TYPE";E[E.RANGE_NOT_SATISFIABLE=416]="RANGE_NOT_SATISFIABLE";E[E.EXPECTATION_FAILED=417]="EXPECTATION_FAILED";E[E.IM_A_TEAPOT=418]="IM_A_TEAPOT";E[E.MISDIRECTED_REQUEST=421]="MISDIRECTED_REQUEST";E[E.UNPROCESSABLE_ENTITY=422]="UNPROCESSABLE_ENTITY";E[E.LOCKED=423]="LOCKED";E[E.FAILED_DEPENDENCY=424]="FAILED_DEPENDENCY";E[E.TOO_EARLY=425]="TOO_EARLY";E[E.UPGRADE_REQUIRED=426]="UPGRADE_REQUIRED";E[E.PRECONDITION_REQUIRED=428]="PRECONDITION_REQUIRED";E[E.TOO_MANY_REQUESTS=429]="TOO_MANY_REQUESTS";E[E.REQUEST_HEADER_FIELDS_TOO_LARGE=431]="REQUEST_HEADER_FIELDS_TOO_LARGE";E[E.UNAVAILABLE_FOR_LEGAL_REASONS=451]="UNAVAILABLE_FOR_LEGAL_REASONS";E[E.INTERNAL_SERVER_ERROR=500]="INTERNAL_SERVER_ERROR";E[E.NOT_IMPLEMENTED=501]="NOT_IMPLEMENTED";E[E.BAD_GATEWAY=502]="BAD_GATEWAY";E[E.SERVICE_UNAVAILABLE=503]="SERVICE_UNAVAILABLE";E[E.GATEWAY_TIMEOUT=504]="GATEWAY_TIMEOUT";E[E.HTTP_VERSION_NOT_SUPPORTED=505]="HTTP_VERSION_NOT_SUPPORTED";E[E.VARIANT_ALSO_NEGOTIATES=506]="VARIANT_ALSO_NEGOTIATES";E[E.INSUFFICIENT_STORAGE=507]="INSUFFICIENT_STORAGE";E[E.LOOP_DETECTED=508]="LOOP_DETECTED";E[E.NOT_EXTENDED=510]="NOT_EXTENDED";E[E.NETWORK_AUTHENTICATION_REQUIRED=511]="NETWORK_AUTHENTICATION_REQUIRED"})(D||={});var P;((O)=>{O.HTML="text/html";O.PLAIN="text/plain";O.CSV="text/csv";O.CSS="text/css";O.JAVASCRIPT="text/javascript";O.JSON="application/json";O.XML="application/xml";O.PDF="application/pdf";O.ZIP="application/zip";O.GZIP="application/gzip";O.OCTET_STREAM="application/octet-stream";O.JPEG="image/jpeg";O.PNG="image/png";O.GIF="image/gif";O.SVG="image/svg+xml";O.WEBP="image/webp";O.MP4="video/mp4";O.WEBM="video/webm";O.MP3="audio/mpeg";O.OGG="audio/ogg";O.WAV="audio/wav";O.ICO="image/x-icon";O.TAR="application/x-tar"})(P||={});var C;((R)=>{R.GET="GET";R.POST="POST";R.PUT="PUT";R.DELETE="DELETE";R.PATCH="PATCH";R.OPTIONS="OPTIONS";R.HEAD="HEAD"})(C||={});var L;((_)=>{_.GZIP="gzip";_.DEFLATE="deflate"})(L||={});function Y(A,I){switch(I){case"gzip":case"deflate":return new Blob([JSON.stringify(A)]).stream().pipeThrough(new CompressionStream(I));default:return A}}var Q=async(A,I,_,G)=>{try{let{body:N,contentType:g,accept:U,compression:R}=_,F=N?Y(N,R):void 0,{signal:x}=G,T=await fetch(A,{signal:x,method:I,body:F,headers:new Headers({"Content-Type":g,Accept:U,"Content-Encoding":R})});if(!T.ok)return console.error(`Encountered error ${T.status}`),{data:null,status:T.status};switch(U){case"application/json":return await T.json();case"text/plain":case"text/html":case"text/csv":case"application/xml":case"text/css":case"text/javascript":return{data:await T.text(),status:T.status};default:return{data:await T.blob(),status:T.status}}}catch(N){return console.error(N.message),{data:null,status:500}}},V=Q;function B(A){console.log(`Path: ${new URL(A.url).pathname}`),console.log(`Method: ${A.method}`),console.log("Headers:"),A.headers.forEach((I,_)=>console.log(`${_}=${I}`))}export{B as logRequest,V as apiCall,P as MimeType,D as HttpStatus,C as HttpMethod,L as CompressionMethod};