Skip to content

Commit

Permalink
feat(types): .DEFAULTS
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 11, 2021
1 parent ed77bdd commit 911e9fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
14 changes: 14 additions & 0 deletions packages/endpoint/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,17 @@ export function apiWithDefaults() {
expectType<"GET">(options2.method);
expectType<string>(options2.url);
}

export function apiDEFAULTS() {
expectType<"https://api.github.com">(endpoint.DEFAULTS.baseUrl);
const myEndpoint = endpoint.withDefaults({
baseUrl: "https://github-enterprise.acme-inc.com/api/v3",
});

myEndpoint.DEFAULTS;

expectType<"https://github-enterprise.acme-inc.com/api/v3">(
// @ts-expect-error - TODO: fix this
myEndpoint.DEFAULTS.baseUrl
);
}
43 changes: 37 additions & 6 deletions packages/types/endpoint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@ type GenericRequestOptions = {
method: Octokit.RequestMethod;
url: string;
headers: Octokit.RequestHeaders;
baseUrl?: string;
mediaType?: {
previews?: string[];
format?: string;
};
data?: unknown;
request?: Octokit.RequestOptions;
};

type EndpointParameters<
type KnownEndpointParameters<
TVersion extends keyof Octokit.ApiVersions = "github.com"
> = { request?: Octokit.RequestOptions<TVersion> } & Record<string, unknown>;
> = { request?: Octokit.RequestOptions<TVersion> } & Partial<
Omit<GenericRequestOptions, "request">
>;

type GLOBAL_DEFAULTS = {
method: "GET";
baseUrl: "https://api.github.com";
headers: {
accept: "application/vnd.github.v3+json";
"user-agent": string;
};
mediaType: {
format: "";
previews: [];
};
};

/**
* The `EndpointInterface` is used for both the standalone `@octokit-next/endpoint` module
Expand All @@ -35,7 +55,8 @@ type EndpointParameters<
* 3. When no endpoint types are imported, then any route with any parameters can be passed in, and the response is unknown.
*/
export interface EndpointInterface<
TVersion extends keyof Octokit.ApiVersions = "github.com"
TVersion extends keyof Octokit.ApiVersions = "github.com",
TDefaults extends KnownEndpointParameters<TVersion> = GLOBAL_DEFAULTS
> {
/**
* Send a request to a known endpoint for the version specified in `request.version`.
Expand Down Expand Up @@ -102,9 +123,19 @@ export interface EndpointInterface<
): GenericRequestOptions;

/**
* Override or set default options
*
* @todo implement inheriting the request version and .DEFAULTS from the options passed
*/
withDefaults<TOptions extends KnownEndpointParameters<TVersion>>(
options: TOptions
): EndpointInterface<TVersion, Omit<TDefaults, keyof TOptions> & TOptions>;

/**
* The current default options
*
* @todo should have proper default values such as `https://api.github.com` and `GET`
* and should be merged when set by `withDefaults()`
*/
withDefaults(
options: EndpointParameters<TVersion>
): EndpointInterface<TVersion>;
DEFAULTS: TDefaults;
}

0 comments on commit 911e9fe

Please sign in to comment.