@quoll/client-lib
Advanced tools
Comparing version 0.4.12 to 0.4.13
@@ -6,2 +6,6 @@ # Change Log | ||
## [0.4.13](https://github.com/mzogheib/quoll/compare/@quoll/client-lib@0.4.12...@quoll/client-lib@0.4.13) (2024-07-05) | ||
**Note:** Version bump only for package @quoll/client-lib | ||
## [0.4.12](https://github.com/mzogheib/quoll/compare/@quoll/client-lib@0.4.11...@quoll/client-lib@0.4.12) (2024-07-05) | ||
@@ -8,0 +12,0 @@ |
@@ -7,3 +7,3 @@ import { User } from "@quoll/lib"; | ||
isAuthenticating: boolean; | ||
user: User | undefined; | ||
user: User | null; | ||
}; | ||
@@ -10,0 +10,0 @@ type UserActions = { |
@@ -1,5 +0,8 @@ | ||
type RequestParams = { | ||
type Headers = Record<string, string>; | ||
type Params = Record<string, string>; | ||
export type RequestParams = { | ||
method: "GET" | "PUT" | "POST" | "DELETE"; | ||
endpoint: string; | ||
params?: Record<string, string>; | ||
headers?: Headers; | ||
params?: Params; | ||
payload?: object; | ||
@@ -21,4 +24,4 @@ }; | ||
*/ | ||
protected request<Response>({ method, endpoint, params, payload, }: RequestParams): Promise<Response>; | ||
protected request<Response>({ method, endpoint, headers, params, payload, }: RequestParams): Promise<Response>; | ||
} | ||
export {}; |
@@ -13,2 +13,5 @@ "use strict"; | ||
exports.ApiService = void 0; | ||
const baseHeaders = { | ||
"Content-Type": "application/json", | ||
}; | ||
class ApiService { | ||
@@ -36,11 +39,10 @@ constructor(baseUrl) { | ||
*/ | ||
request({ method, endpoint, params, payload, }) { | ||
request({ method, endpoint, headers, params, payload, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.makeUrl(endpoint, params); | ||
const allHeaders = Object.assign(Object.assign({}, baseHeaders), headers); | ||
const init = { | ||
method, | ||
body: JSON.stringify(payload), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
headers: allHeaders, | ||
}; | ||
@@ -47,0 +49,0 @@ const response = yield fetch(url, init); |
@@ -1,25 +0,6 @@ | ||
type RequestParams = { | ||
method: "GET" | "PUT" | "POST" | "DELETE"; | ||
endpoint: string; | ||
params?: Record<string, string>; | ||
payload?: object; | ||
}; | ||
export declare abstract class AuthenticatedApiService { | ||
private baseUrl; | ||
import { ApiService, RequestParams } from "../api"; | ||
export declare abstract class AuthenticatedApiService extends ApiService { | ||
private getAccessToken; | ||
constructor(getAccessToken: () => Promise<string>, baseUrl: string); | ||
private makeUrl; | ||
private makeHeaders; | ||
/** | ||
* If the endpoint will not return any content then `Response` should be | ||
* `null`. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* const data = authenticatedApiService.request<null>({ ... }) | ||
* ``` | ||
*/ | ||
protected request<Response>({ method, endpoint, params, payload, }: RequestParams): Promise<Response>; | ||
protected request<Response>(params: RequestParams): Promise<Response>; | ||
} | ||
export {}; |
@@ -11,63 +11,33 @@ "use strict"; | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AuthenticatedApiService = void 0; | ||
class AuthenticatedApiService { | ||
const api_1 = require("../api"); | ||
class AuthenticatedApiService extends api_1.ApiService { | ||
constructor(getAccessToken, baseUrl) { | ||
super(baseUrl); | ||
this.getAccessToken = getAccessToken; | ||
this.baseUrl = baseUrl; | ||
} | ||
makeUrl(endpoint, params) { | ||
const baseUrl = `${this.baseUrl}${endpoint}`; | ||
const search = new URLSearchParams(params); | ||
const searchString = search.toString(); | ||
if (searchString) | ||
return `${baseUrl}?${searchString}`; | ||
return baseUrl; | ||
} | ||
makeHeaders() { | ||
request(params) { | ||
const _super = Object.create(null, { | ||
request: { get: () => super.request } | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { headers } = params, rest = __rest(params, ["headers"]); | ||
const accessToken = yield this.getAccessToken(); | ||
return { | ||
"Content-Type": "application/json", | ||
authorization: `Bearer ${accessToken}`, | ||
}; | ||
const allHeaders = Object.assign(Object.assign({}, headers), { authorization: `Bearer ${accessToken}` }); | ||
return yield _super.request.call(this, Object.assign(Object.assign({}, rest), { headers: allHeaders })); | ||
}); | ||
} | ||
/** | ||
* If the endpoint will not return any content then `Response` should be | ||
* `null`. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* const data = authenticatedApiService.request<null>({ ... }) | ||
* ``` | ||
*/ | ||
request({ method, endpoint, params, payload, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const url = this.makeUrl(endpoint, params); | ||
const init = { | ||
method, | ||
body: JSON.stringify(payload), | ||
headers: yield this.makeHeaders(), | ||
}; | ||
const response = yield fetch(url, init); | ||
const contentHeader = response.headers.get("Content-Length"); | ||
const hasContent = contentHeader && contentHeader !== "0"; | ||
// The caller should know whether or not the response has content. | ||
// If it does, the `Response` type will be set as non-null. | ||
// If it does not, the `Response` type will be set as `null`. | ||
// Hence the value of responseJson will always match the caller's intention. | ||
const responseJson = hasContent ? yield response.json() : null; | ||
if (response.ok) | ||
return responseJson; | ||
const error = { | ||
status: response.status, | ||
statusText: response.statusText, | ||
body: responseJson, | ||
}; | ||
throw new Error(JSON.stringify(error)); | ||
}); | ||
} | ||
} | ||
exports.AuthenticatedApiService = AuthenticatedApiService; |
{ | ||
"name": "@quoll/client-lib", | ||
"version": "0.4.12", | ||
"version": "0.4.13", | ||
"description": "Shared code for client side packages", | ||
@@ -31,3 +31,3 @@ "repository": "https://github.com/mzogheib/quoll", | ||
}, | ||
"gitHead": "b7a10dcf283ea223699de15de57db153134f860e" | ||
"gitHead": "beb4eb8c67f63717daa198e32e42c6f496cf7370" | ||
} |
@@ -9,3 +9,3 @@ import { User } from "@quoll/lib"; | ||
isAuthenticating: boolean; | ||
user: User | undefined; | ||
user: User | null; | ||
}; | ||
@@ -12,0 +12,0 @@ |
@@ -1,8 +0,17 @@ | ||
type RequestParams = { | ||
type Headers = Record<string, string>; | ||
type Params = Record<string, string>; | ||
export type RequestParams = { | ||
method: "GET" | "PUT" | "POST" | "DELETE"; | ||
endpoint: string; | ||
params?: Record<string, string>; | ||
headers?: Headers; | ||
params?: Params; | ||
payload?: object; | ||
}; | ||
const baseHeaders = { | ||
"Content-Type": "application/json", | ||
}; | ||
export abstract class ApiService { | ||
@@ -15,3 +24,3 @@ private baseUrl: string; | ||
private makeUrl(endpoint: string, params?: Record<string, string>) { | ||
private makeUrl(endpoint: string, params?: Params) { | ||
const baseUrl = `${this.baseUrl}${endpoint}`; | ||
@@ -40,2 +49,3 @@ | ||
endpoint, | ||
headers, | ||
params, | ||
@@ -45,8 +55,12 @@ payload, | ||
const url = this.makeUrl(endpoint, params); | ||
const allHeaders = { | ||
...baseHeaders, | ||
...headers, | ||
}; | ||
const init = { | ||
method, | ||
body: JSON.stringify(payload), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
headers: allHeaders, | ||
}; | ||
@@ -53,0 +67,0 @@ |
@@ -1,81 +0,23 @@ | ||
type RequestParams = { | ||
method: "GET" | "PUT" | "POST" | "DELETE"; | ||
endpoint: string; | ||
params?: Record<string, string>; | ||
payload?: object; | ||
}; | ||
import { ApiService, RequestParams } from "../api"; | ||
export abstract class AuthenticatedApiService { | ||
private baseUrl: string; | ||
export abstract class AuthenticatedApiService extends ApiService { | ||
private getAccessToken: () => Promise<string>; | ||
constructor(getAccessToken: () => Promise<string>, baseUrl: string) { | ||
super(baseUrl); | ||
this.getAccessToken = getAccessToken; | ||
this.baseUrl = baseUrl; | ||
} | ||
private makeUrl(endpoint: string, params?: Record<string, string>) { | ||
const baseUrl = `${this.baseUrl}${endpoint}`; | ||
protected async request<Response>(params: RequestParams) { | ||
const { headers, ...rest } = params; | ||
const search = new URLSearchParams(params); | ||
const searchString = search.toString(); | ||
if (searchString) return `${baseUrl}?${searchString}`; | ||
return baseUrl; | ||
} | ||
private async makeHeaders() { | ||
const accessToken = await this.getAccessToken(); | ||
return { | ||
"Content-Type": "application/json", | ||
const allHeaders = { | ||
...headers, | ||
authorization: `Bearer ${accessToken}`, | ||
}; | ||
} | ||
/** | ||
* If the endpoint will not return any content then `Response` should be | ||
* `null`. | ||
* | ||
* @example | ||
* | ||
* ``` | ||
* const data = authenticatedApiService.request<null>({ ... }) | ||
* ``` | ||
*/ | ||
protected async request<Response>({ | ||
method, | ||
endpoint, | ||
params, | ||
payload, | ||
}: RequestParams) { | ||
const url = this.makeUrl(endpoint, params); | ||
const init = { | ||
method, | ||
body: JSON.stringify(payload), | ||
headers: await this.makeHeaders(), | ||
}; | ||
const response = await fetch(url, init); | ||
const contentHeader = response.headers.get("Content-Length"); | ||
const hasContent = contentHeader && contentHeader !== "0"; | ||
// The caller should know whether or not the response has content. | ||
// If it does, the `Response` type will be set as non-null. | ||
// If it does not, the `Response` type will be set as `null`. | ||
// Hence the value of responseJson will always match the caller's intention. | ||
const responseJson: Response = hasContent ? await response.json() : null; | ||
if (response.ok) return responseJson; | ||
const error = { | ||
status: response.status, | ||
statusText: response.statusText, | ||
body: responseJson, | ||
}; | ||
throw new Error(JSON.stringify(error)); | ||
return await super.request<Response>({ ...rest, headers: allHeaders }); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92534
71
1
3
70
1442