@auldrant/api 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/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +110 -0
- package/dist/index.js +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Adam Hughes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @auldrant/api
|
|
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
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Static Content
|
|
10
|
+
|
|
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.
|
|
12
|
+
|
|
13
|
+
## Methods
|
|
14
|
+
|
|
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.
|
|
16
|
+
|
|
17
|
+
[static]: ./src/static.ts
|
|
18
|
+
[apiCall]: ./src/apiCall.ts
|
|
19
|
+
[fetch]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
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 {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
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};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@auldrant/api",
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"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
|
+
"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",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/coloneljade/auldrant-api/issues",
|
|
25
|
+
"email": "colonel.jade@proton.me"
|
|
26
|
+
},
|
|
27
|
+
"description": "Simple library for working with APIs",
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/**/*.js",
|
|
30
|
+
"dist/**/*.d.ts",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "bun clean && bun ./build.ts",
|
|
38
|
+
"clean": "bun ./clean.ts --dir dist",
|
|
39
|
+
"publish": "bun test && bun publish --frozen-lockfile"
|
|
40
|
+
}
|
|
41
|
+
}
|