Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: Configuration) {
this.configuration = configuration;
Expand Down Expand Up @@ -82,8 +86,10 @@ export class BaseAPI {

private static readonly jsonRegex = /^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$/i;
private middleware: Middleware[];
protected configuration: Configuration;

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -248,8 +254,10 @@ function isFormData(value: any): value is FormData {

export class ResponseError extends Error {
override name: "ResponseError" = "ResponseError";
constructor(public response: Response, msg?: string) {
response: Response;
constructor(response: Response, msg?: string) {
super(msg);
this.response = response;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -259,10 +267,17 @@ export class ResponseError extends Error {
}
}

// `cause` is declared through an interface so that it stays compatible with lib targets
// that already declare `Error.cause` (ES2022+) as well as the ones that do not
export interface FetchError {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this interface? if we need it, should it have a different name?

cause: Error;
}

export class FetchError extends Error {
override name: "FetchError" = "FetchError";
constructor(public cause: Error, msg?: string) {
constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -274,8 +289,10 @@ export class FetchError extends Error {

export class RequiredError extends Error {
override name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
field: string;
constructor(field: string, msg?: string) {
super(msg);
this.field = field;

// restore prototype chain
const actualProto = new.target.prototype;
Expand Down Expand Up @@ -414,31 +431,49 @@ export interface ResponseTransformer<T> {
}

export class JSONApiResponse<T> {
constructor(public raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {}
raw: Response;
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {
this.raw = raw;
this.transformer = transformer;
}

async value(): Promise<T> {
return this.transformer(await this.raw.json());
}
}

export class VoidApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<void> {
return undefined;
}
}

export class BlobApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<Blob> {
return await this.raw.blob();
};
}

export class TextApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<string> {
return await this.raw.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: Configuration) {
this.configuration = configuration;
Expand Down Expand Up @@ -92,8 +96,10 @@ export class BaseAPI {

private static readonly jsonRegex = /^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$/i;
private middleware: Middleware[];
protected configuration: Configuration;

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -258,8 +264,10 @@ function isFormData(value: any): value is FormData {

export class ResponseError extends Error {
override name: "ResponseError" = "ResponseError";
constructor(public response: Response, msg?: string) {
response: Response;
constructor(response: Response, msg?: string) {
super(msg);
this.response = response;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -269,10 +277,17 @@ export class ResponseError extends Error {
}
}

// `cause` is declared through an interface so that it stays compatible with lib targets
// that already declare `Error.cause` (ES2022+) as well as the ones that do not
export interface FetchError {
cause: Error;
}

export class FetchError extends Error {
override name: "FetchError" = "FetchError";
constructor(public cause: Error, msg?: string) {
constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -284,8 +299,10 @@ export class FetchError extends Error {

export class RequiredError extends Error {
override name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
field: string;
constructor(field: string, msg?: string) {
super(msg);
this.field = field;

// restore prototype chain
const actualProto = new.target.prototype;
Expand Down Expand Up @@ -422,31 +439,49 @@ export interface ResponseTransformer<T> {
}

export class JSONApiResponse<T> {
constructor(public raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {}
raw: Response;
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {
this.raw = raw;
this.transformer = transformer;
}

async value(): Promise<T> {
return this.transformer(await this.raw.json());
}
}

export class VoidApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<void> {
return undefined;
}
}

export class BlobApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<Blob> {
return await this.raw.blob();
};
}

export class TextApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<string> {
return await this.raw.text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export interface ConfigurationParameters {
}

export class Configuration {
constructor(private configuration: ConfigurationParameters = {}) {}
private configuration: ConfigurationParameters;

constructor(configuration: ConfigurationParameters = {}) {
this.configuration = configuration;
}

set config(configuration: Configuration) {
this.configuration = configuration;
Expand Down Expand Up @@ -92,8 +96,10 @@ export class BaseAPI {

private static readonly jsonRegex = /^(:?application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(:?;.*)?$/i;
private middleware: Middleware[];
protected configuration: Configuration;

constructor(protected configuration = DefaultConfig) {
constructor(configuration: Configuration = DefaultConfig) {
this.configuration = configuration;
this.middleware = configuration.middleware;
}

Expand Down Expand Up @@ -258,8 +264,10 @@ function isFormData(value: any): value is FormData {

export class ResponseError extends Error {
override name: "ResponseError" = "ResponseError";
constructor(public response: Response, msg?: string) {
response: Response;
constructor(response: Response, msg?: string) {
super(msg);
this.response = response;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -269,10 +277,17 @@ export class ResponseError extends Error {
}
}

// `cause` is declared through an interface so that it stays compatible with lib targets
// that already declare `Error.cause` (ES2022+) as well as the ones that do not
export interface FetchError {
cause: Error;
}

export class FetchError extends Error {
override name: "FetchError" = "FetchError";
constructor(public cause: Error, msg?: string) {
constructor(cause: Error, msg?: string) {
super(msg);
this.cause = cause;

// restore prototype chain
const actualProto = new.target.prototype;
Expand All @@ -284,8 +299,10 @@ export class FetchError extends Error {

export class RequiredError extends Error {
override name: "RequiredError" = "RequiredError";
constructor(public field: string, msg?: string) {
field: string;
constructor(field: string, msg?: string) {
super(msg);
this.field = field;

// restore prototype chain
const actualProto = new.target.prototype;
Expand Down Expand Up @@ -422,31 +439,49 @@ export interface ResponseTransformer<T> {
}

export class JSONApiResponse<T> {
constructor(public raw: Response, private transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {}
raw: Response;
private transformer: ResponseTransformer<T>;

constructor(raw: Response, transformer: ResponseTransformer<T> = (jsonValue: any) => jsonValue) {
this.raw = raw;
this.transformer = transformer;
}

async value(): Promise<T> {
return this.transformer(await this.raw.json());
}
}

export class VoidApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<void> {
return undefined;
}
}

export class BlobApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<Blob> {
return await this.raw.blob();
};
}

export class TextApiResponse {
constructor(public raw: Response) {}
raw: Response;

constructor(raw: Response) {
this.raw = raw;
}

async value(): Promise<string> {
return await this.raw.text();
Expand Down
Loading
Loading