Skip to content

Commit

Permalink
feat(core): the client now supports alias method calls
Browse files Browse the repository at this point in the history
chore: better typing support
  • Loading branch information
Michael committed Oct 21, 2022
1 parent 02cf22c commit fd7dce4
Showing 1 changed file with 147 additions and 17 deletions.
164 changes: 147 additions & 17 deletions lib/core/urbex.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,87 @@
import type { RequestUrlPath } from "../types";
import type { UrbexClientConfig } from "./config/request-config";
import type { Methods, MethodsUpper, RequestUrlPath } from "../types";
import type {
UrbexClientOptions,
URIComponent,
DispatchedResponse,
UrbexURL,
PassableConfig,
InternalUrbexConfiguration
} from "./types";

import { RequestApi } from "./api/request-api";
import { RequestConfig } from "./config/request-config";
import {
createPromise,
deepMerge,
merge,
clone,
isString,
isObject,
argumentIsNotProvided,
hasOwnProperty,
stringReplacer,
ensureLeadingSlash,
forEach,
isUndefined,
uppercase
} from "../utils";
import {
convertStringToURIComponent,
convertURIComponentToString
} from "./url";
import { METHODS } from "../constants";

type NullableRequestBody = Omit<PassableConfig, "data">;

export interface UrbexClient {
/**
* Send a GET request.
*/
get(url: UrbexURL, config?: NullableRequestBody): DispatchedResponse;
/**
* Send a POST request.
*/
post(
url: UrbexURL,
data?: any,
config?: NullableRequestBody
): DispatchedResponse;
/**
* Send a PUT request.
*/
put(
url: UrbexURL,
data?: any,
config?: NullableRequestBody
): DispatchedResponse;
/**
* Send a PATCH request.
*/
patch(
url: UrbexURL,
data?: any,
config?: NullableRequestBody
): DispatchedResponse;
/**
* Send a DELETE request.
*/
delete(url: UrbexURL, config?: NullableRequestBody): DispatchedResponse;
/**
* Send a HEAD request.
*/
head(url: UrbexURL, config?: NullableRequestBody): DispatchedResponse;
/**
* Send a OPTIONS request.
*/
options(url: UrbexURL, config?: NullableRequestBody): DispatchedResponse;
}

export class UrbexClient extends RequestApi {
private $config: RequestConfig;
private $interceptors = {};
private $subscriptions = {};

constructor(config?: UrbexClientConfig) {
constructor(config?: UrbexClientOptions) {
super();

this.$config = new RequestConfig(config);
Expand All @@ -19,37 +91,95 @@ export class UrbexClient extends RequestApi {
*
* Creates a new instance of the UrbexClient.
*/
static create(): UrbexClient {
return new UrbexClient();
static create(config?: UrbexClientOptions): UrbexClient {
return new UrbexClient(config);
}

get config(): UrbexClientConfig {
private createMethodConfig(
method: Methods,
uri: UrbexURL,
config: PassableConfig
): UrbexClientOptions {
if (argumentIsNotProvided(uri)) {
throw new Error(
"Attempted to call a HTTP method without providing a URL. If you want to use the default URL, use `urbex.send` instead."
);
}

return merge(config, { url: uri, method: method });
}

get config(): Readonly<InternalUrbexConfiguration> {
return this.$config.get();
}

/**
* Configures the UrbexClient.
* Configures the UrbexClient. You are free to call this method as
* many times as you want. All configurations will be merged together.
*
*
* @param config The configuration to use.
*/
public configure(config: UrbexClientConfig): void {
this.$config.set(config);
public configure(config: UrbexClientOptions): void {
this.$config.set(config, false);
}

public get() {}
public send(config?: UrbexClientOptions): DispatchedResponse {
// https://github.com/orison-networks/urbex/issues/4
if (isString(config.url) && config.url.startsWith("/")) {
config.url = {
endpoint: config.url
};
}

public post() {}
const cfg = this.$config.merge(
this.$config.parseIncomingConfig(config, true)
) as InternalUrbexConfiguration;

public put() {}
return this.dispatchRequest(cfg);
}

public patch() {}
/**
* When a response is received, the UrbexClient will actively push out the response to all active
* subscriptions
*/
public subscribe() {}
}

public delete() {}
forEach(["delete", "get", "head", "options"], (_, value: MethodsUpper) => {
UrbexClient.prototype[value] = function (
url: UrbexURL,
config?: NullableRequestBody
) {
return this.send(
this.createMethodConfig(uppercase(value), url, config)
);
};
});

public head() {}
forEach(["post", "put", "patch"], (_, value: MethodsUpper) => {
UrbexClient.prototype[value] = function (
url: UrbexURL,
data?: any,
config?: NullableRequestBody
) {
function combineIncomingConfig(): PassableConfig {
if (!isUndefined(data)) {
if (isObject(config)) {
return merge(config, { data: data });
} else {
return { data };
}
}

public options() {}
}
return config;
}

const cfg = combineIncomingConfig();

return this.send(this.createMethodConfig(uppercase(value), url, cfg));
};
});

export function isUrbexClient(client: unknown): client is UrbexClient {
return client instanceof UrbexClient;
Expand Down

0 comments on commit fd7dce4

Please sign in to comment.