@brickninjaapi/fetch 0.0.19 → 0.0.23
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/CHANGELOG.md +30 -0
- package/package.json +6 -5
- package/dist/index.d.ts +0 -29
- package/dist/index.js +0 -87
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
# @brickninjaapi/fetch
|
|
2
2
|
|
|
3
|
+
## 0.0.23
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 06e285a: fix(gha): update publish config
|
|
8
|
+
|
|
9
|
+
## 0.0.22
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- f6c3e57: Bump versions
|
|
14
|
+
|
|
15
|
+
## 0.0.21
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Bump versions
|
|
20
|
+
|
|
21
|
+
## 0.0.20
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Migrate to pnpm
|
|
26
|
+
|
|
27
|
+
## 0.0.20
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- Update versions
|
|
32
|
+
|
|
3
33
|
## 0.0.19
|
|
4
34
|
|
|
5
35
|
### Patch Changes
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brickninjaapi/fetch",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "Tiny wrapper around fetch that returns type-safe responses",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/brickninja-org/
|
|
10
|
+
"url": "git+https://github.com/brickninja-org/brickninja-api-ts.git",
|
|
11
11
|
"directory": "packages/fetch"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
@@ -21,12 +21,13 @@
|
|
|
21
21
|
"build": "tsc"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"typescript": "5.
|
|
24
|
+
"typescript": "5.9.3"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@brickninjaapi/types": "~0.0.
|
|
27
|
+
"@brickninjaapi/types": "~0.0.26"
|
|
28
28
|
},
|
|
29
29
|
"publishConfig": {
|
|
30
|
-
"access": "public"
|
|
30
|
+
"access": "public",
|
|
31
|
+
"provenance": true
|
|
31
32
|
}
|
|
32
33
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { EndpointType, KnownEndpoint, OptionsByEndpoint } from '@brickninjaapi/types/endpoints';
|
|
2
|
-
import type { SchemaVersion } from '@brickninjaapi/types/schema';
|
|
3
|
-
type RequiredKeys<T> = {
|
|
4
|
-
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
5
|
-
}[keyof T];
|
|
6
|
-
type Args<Url extends string, Schema extends SchemaVersion> = RequiredKeys<OptionsByEndpoint<Url>> extends never ? [endpoint: Url, options?: FetchBrickNinjaApiOptions<Schema> & OptionsByEndpoint<Url> & FetchOptions] : [endpoint: Url, options: FetchBrickNinjaApiOptions<Schema> & OptionsByEndpoint<Url> & FetchOptions];
|
|
7
|
-
export declare function fetchBrickNinjaApi<Url extends KnownEndpoint | (string & {}), Schema extends SchemaVersion = undefined>(...[endpoint, options]: Args<Url, Schema>): Promise<EndpointType<Url, Schema>>;
|
|
8
|
-
export type FetchBrickNinjaApiOptions<Schema extends SchemaVersion> = {
|
|
9
|
-
/** The schema to use when making the API request */
|
|
10
|
-
schema?: Schema;
|
|
11
|
-
/** onRequest handler allows to modify the request made to the Brick Ninja API. */
|
|
12
|
-
onRequest?: (request: Request) => Request | Promise<Request>;
|
|
13
|
-
/**
|
|
14
|
-
* onResponse handler. Called for all responses, successful or not.
|
|
15
|
-
* Make sure to clone the response in case of consuming the body.
|
|
16
|
-
*/
|
|
17
|
-
onResponse?: (response: Response) => void | Promise<void>;
|
|
18
|
-
};
|
|
19
|
-
export type FetchOptions = {
|
|
20
|
-
/** @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal */
|
|
21
|
-
signal?: AbortSignal;
|
|
22
|
-
/** @see https://developer.mozilla.org/en-US/docs/Web/API/Request/cache */
|
|
23
|
-
cache?: RequestCache;
|
|
24
|
-
};
|
|
25
|
-
export declare class BrickNinjaApiError extends Error {
|
|
26
|
-
response: Response;
|
|
27
|
-
constructor(message: string, response: Response);
|
|
28
|
-
}
|
|
29
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
export function fetchBrickNinjaApi() {
|
|
11
|
-
return __awaiter(this, arguments, void 0, function* (...[endpoint, options]) {
|
|
12
|
-
var _a;
|
|
13
|
-
const url = new URL(endpoint, 'https://brick-ninja-api.vercel.app/');
|
|
14
|
-
if (options.schema) {
|
|
15
|
-
url.searchParams.set('v', options.schema);
|
|
16
|
-
}
|
|
17
|
-
if (hasLanguage(options)) {
|
|
18
|
-
url.searchParams.set('lang', options.language);
|
|
19
|
-
}
|
|
20
|
-
if (hasAccessToken(options)) {
|
|
21
|
-
url.searchParams.set('access_token', options.accessToken);
|
|
22
|
-
}
|
|
23
|
-
// build request
|
|
24
|
-
let request = new Request(url, {
|
|
25
|
-
// The Brick Ninja API never uses redirects, so we want to error if we encounter one.
|
|
26
|
-
// We use `manual` instead of `error` here so we can throw our own `BrickNinjaApiError` with the response attached
|
|
27
|
-
redirect: 'manual',
|
|
28
|
-
// set signal and cache from options
|
|
29
|
-
signal: options.signal,
|
|
30
|
-
cache: options.cache
|
|
31
|
-
});
|
|
32
|
-
// if there is a onRequest handler registered, let it modify the request
|
|
33
|
-
if (options.onRequest) {
|
|
34
|
-
request = yield options.onRequest(request);
|
|
35
|
-
if (!(request instanceof Request)) {
|
|
36
|
-
throw new Error(`onRequest has to return a Request`);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
// call the API
|
|
40
|
-
const response = yield fetch(request);
|
|
41
|
-
// call onResponse handler
|
|
42
|
-
yield ((_a = options.onResponse) === null || _a === void 0 ? void 0 : _a.call(options, response));
|
|
43
|
-
// check if the response is json (`application/json; charset=utf-8`)
|
|
44
|
-
const isJson = response.headers.get('content-type').startsWith('application/json');
|
|
45
|
-
// censor access token in url to not leak it in error messages
|
|
46
|
-
const erroredUrl = hasAccessToken(options)
|
|
47
|
-
? url.toString().replace(options.accessToken, '***')
|
|
48
|
-
: url.toString();
|
|
49
|
-
// check if the response is an error
|
|
50
|
-
if (!response.ok) {
|
|
51
|
-
// if the response is JSON, it might have more details in the `text` prop
|
|
52
|
-
if (isJson) {
|
|
53
|
-
const error = yield response.json();
|
|
54
|
-
if (typeof error === 'object' && 'text' in error && typeof error.text === 'string') {
|
|
55
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' returned ${response.status} ${response.statusText}: ${error.text}.`, response);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
// otherwise just throw error with the status code
|
|
59
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' returned ${response.status} ${response.statusText}.`, response);
|
|
60
|
-
}
|
|
61
|
-
// if the response is not JSON, throw an error
|
|
62
|
-
if (!isJson) {
|
|
63
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' did not respond with a JSON response`, response);
|
|
64
|
-
}
|
|
65
|
-
// parse json
|
|
66
|
-
const json = yield response.json();
|
|
67
|
-
// check that json is not `["v1", "v2"]` which sometimes happens for authenticated endpoints
|
|
68
|
-
if (url.toString() !== 'https://brick-ninja-api.vercel.app/' && Array.isArray(json) && json.length === 2 && json[0] === 'v1' && json[1] === 'v2') {
|
|
69
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' did returned an invalid response (["v1", "v2"])`, response);
|
|
70
|
-
}
|
|
71
|
-
// TODO: catch more errors
|
|
72
|
-
return json;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
export class BrickNinjaApiError extends Error {
|
|
76
|
-
constructor(message, response) {
|
|
77
|
-
super(message);
|
|
78
|
-
this.response = response;
|
|
79
|
-
this.name = 'BrickNinjaApiError';
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function hasLanguage(options) {
|
|
83
|
-
return 'language' in options;
|
|
84
|
-
}
|
|
85
|
-
function hasAccessToken(options) {
|
|
86
|
-
return 'accessToken' in options;
|
|
87
|
-
}
|