@brickninjaapi/fetch 0.0.3 → 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/CHANGELOG.md +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -5
- package/package.json +1 -1
- package/src/index.ts +11 -6
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ type RequiredKeys<T> = {
|
|
|
4
4
|
[K in keyof T]-?: {} extends Pick<T, K> ? never : K;
|
|
5
5
|
}[keyof T];
|
|
6
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 =
|
|
7
|
+
export declare function fetchBrickNinjaApi<Url extends KnownEndpoint | (string & {}), Schema extends SchemaVersion = undefined>(...[endpoint, options]: Args<Url, Schema>): Promise<EndpointType<Url, Schema>>;
|
|
8
8
|
export type FetchBrickNinjaApiOptions<Schema extends SchemaVersion> = {
|
|
9
9
|
/** The schema to use when making the API request */
|
|
10
10
|
schema?: Schema;
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
export function fetchBrickNinjaApi() {
|
|
11
11
|
return __awaiter(this, arguments, void 0, function* (...[endpoint, options]) {
|
|
12
12
|
var _a;
|
|
13
|
-
const url = new URL('https://brick-ninja-api.vercel.app/');
|
|
13
|
+
const url = new URL(endpoint, 'https://brick-ninja-api.vercel.app/');
|
|
14
14
|
if (options.schema) {
|
|
15
15
|
url.searchParams.set('v', options.schema);
|
|
16
16
|
}
|
|
@@ -38,11 +38,11 @@ export function fetchBrickNinjaApi() {
|
|
|
38
38
|
}
|
|
39
39
|
// call the API
|
|
40
40
|
const response = yield fetch(request);
|
|
41
|
-
// call
|
|
41
|
+
// call onResponse handler
|
|
42
42
|
yield ((_a = options.onResponse) === null || _a === void 0 ? void 0 : _a.call(options, response));
|
|
43
43
|
// check if the response is json (`application/json; charset=utf-8`)
|
|
44
44
|
const isJson = response.headers.get('content-type').startsWith('application/json');
|
|
45
|
-
// censor access token in
|
|
45
|
+
// censor access token in url to not leak it in error messages
|
|
46
46
|
const erroredUrl = hasAccessToken(options)
|
|
47
47
|
? url.toString().replace(options.accessToken, '***')
|
|
48
48
|
: url.toString();
|
|
@@ -60,10 +60,14 @@ export function fetchBrickNinjaApi() {
|
|
|
60
60
|
}
|
|
61
61
|
// if the response is not JSON, throw an error
|
|
62
62
|
if (!isJson) {
|
|
63
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}'did not respond with a JSON response
|
|
63
|
+
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' did not respond with a JSON response`, response);
|
|
64
64
|
}
|
|
65
|
-
// parse
|
|
65
|
+
// parse json
|
|
66
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
|
+
}
|
|
67
71
|
// TODO: catch more errors
|
|
68
72
|
return json;
|
|
69
73
|
});
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,11 +10,11 @@ type Args<Url extends string, Schema extends SchemaVersion> = RequiredKeys<Optio
|
|
|
10
10
|
|
|
11
11
|
export async function fetchBrickNinjaApi<
|
|
12
12
|
Url extends KnownEndpoint | (string & {}),
|
|
13
|
-
Schema extends SchemaVersion =
|
|
13
|
+
Schema extends SchemaVersion = undefined
|
|
14
14
|
>(
|
|
15
15
|
...[endpoint, options]: Args<Url, Schema>
|
|
16
16
|
): Promise<EndpointType<Url, Schema>> {
|
|
17
|
-
const url = new URL('https://brick-ninja-api.vercel.app/');
|
|
17
|
+
const url = new URL(endpoint, 'https://brick-ninja-api.vercel.app/');
|
|
18
18
|
|
|
19
19
|
if (options.schema) {
|
|
20
20
|
url.searchParams.set('v', options.schema);
|
|
@@ -49,13 +49,13 @@ export async function fetchBrickNinjaApi<
|
|
|
49
49
|
// call the API
|
|
50
50
|
const response = await fetch(request);
|
|
51
51
|
|
|
52
|
-
// call
|
|
52
|
+
// call onResponse handler
|
|
53
53
|
await options.onResponse?.(response);
|
|
54
54
|
|
|
55
55
|
// check if the response is json (`application/json; charset=utf-8`)
|
|
56
56
|
const isJson = response.headers.get('content-type').startsWith('application/json');
|
|
57
57
|
|
|
58
|
-
// censor access token in
|
|
58
|
+
// censor access token in url to not leak it in error messages
|
|
59
59
|
const erroredUrl = hasAccessToken(options)
|
|
60
60
|
? url.toString().replace(options.accessToken, '***')
|
|
61
61
|
: url.toString();
|
|
@@ -77,12 +77,17 @@ export async function fetchBrickNinjaApi<
|
|
|
77
77
|
|
|
78
78
|
// if the response is not JSON, throw an error
|
|
79
79
|
if (!isJson) {
|
|
80
|
-
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}'did not respond with a JSON response
|
|
80
|
+
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' did not respond with a JSON response`, response);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
// parse
|
|
83
|
+
// parse json
|
|
84
84
|
const json = await response.json();
|
|
85
85
|
|
|
86
|
+
// check that json is not `["v1", "v2"]` which sometimes happens for authenticated endpoints
|
|
87
|
+
if(url.toString() !== 'https://brick-ninja-api.vercel.app/' && Array.isArray(json) && json.length === 2 && json[0] === 'v1' && json[1] === 'v2') {
|
|
88
|
+
throw new BrickNinjaApiError(`The Brick Ninja API call to '${erroredUrl}' did returned an invalid response (["v1", "v2"])`, response);
|
|
89
|
+
}
|
|
90
|
+
|
|
86
91
|
// TODO: catch more errors
|
|
87
92
|
|
|
88
93
|
return json;
|