@octokit-next/types
Advanced tools
Comparing version 1.14.0 to 1.15.0
@@ -1,4 +0,9 @@ | ||
import { SetRequired } from "type-fest"; | ||
import { Octokit } from "./index.js"; | ||
import { | ||
ArgumentsTypesForRoute, | ||
VersionsByRoute, | ||
AllKnownRoutes, | ||
ParametersByVersionAndRoute, | ||
RequestByVersionAndRoute, | ||
} from "./utils"; | ||
@@ -12,2 +17,7 @@ /** | ||
headers: Octokit.RequestHeaders; | ||
baseUrl?: string; | ||
mediaType?: { | ||
previews?: string[]; | ||
format?: string; | ||
}; | ||
data?: unknown; | ||
@@ -17,6 +27,21 @@ 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: []; | ||
}; | ||
}; | ||
/** | ||
@@ -35,3 +60,4 @@ * The `EndpointInterface` is used for both the standalone `@octokit-next/endpoint` module | ||
export interface EndpointInterface< | ||
TVersion extends keyof Octokit.ApiVersions = "github.com" | ||
TVersion extends keyof Octokit.ApiVersions = "github.com", | ||
TDefaults extends KnownEndpointParameters<TVersion> = GLOBAL_DEFAULTS | ||
> { | ||
@@ -101,136 +127,19 @@ /** | ||
): GenericRequestOptions; | ||
} | ||
/** | ||
* optimized type to lookup all known routes and the versions they are supported in. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // import REST API types for github.com, GHES 3.2 and GHES 3.1 | ||
* import "@octokit-next/types-rest-api-ghes-3.1"; | ||
* ``` | ||
* | ||
* The `Octokit.ApiVersions` interface is now looking like this (simplified) | ||
* | ||
* ```ts | ||
* { | ||
* "github.com": { "GET /": { … } }, | ||
* "ghes-3.1": { "GET /": { … }, "GET /admin/tokens": { … } }, | ||
* "ghes-3.2": { "GET /": { … }, "GET /admin/tokens": { … } } | ||
* } | ||
* ``` | ||
* | ||
* The `VersionsByRoute` as a result looks like this | ||
* | ||
* ```ts | ||
* { | ||
* "GET /": "github.com" | "ghes-3.1" | "ghes-3.2", | ||
* "GET /admin/tokens": "ghes-3.1" | "ghes-3.2" | ||
* } | ||
* ``` | ||
*/ | ||
type VersionsByRoute = Remap<EndpointsByVersion>; | ||
/** | ||
* 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>; | ||
// types to improve performance of type lookups | ||
/** | ||
* All known routes across all defined API versions for fast lookup | ||
*/ | ||
type AllKnownRoutes = keyof VersionsByRoute; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { Endpoints: { [route]: Endpoint } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Endpoint } } | ||
* ``` | ||
*/ | ||
type EndpointsByVersion = { | ||
[Version in keyof Octokit.ApiVersions]: "Endpoints" extends keyof Octokit.ApiVersions[Version] | ||
? Octokit.ApiVersions[Version]["Endpoints"] | ||
: never; | ||
}; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { [route]: { parameters: Parameters } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Parameters } } | ||
* ``` | ||
*/ | ||
type ParametersByVersionAndRoute = { | ||
[Version in keyof EndpointsByVersion]: { | ||
[Route in keyof EndpointsByVersion[Version]]: "parameters" extends keyof EndpointsByVersion[Version][Route] | ||
? EndpointsByVersion[Version][Route]["parameters"] & { | ||
headers?: Octokit.RequestHeaders; | ||
} | ||
: never; | ||
}; | ||
}; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { [route]: { request: Request } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Request } } | ||
* ``` | ||
*/ | ||
type RequestByVersionAndRoute = { | ||
[Version in keyof EndpointsByVersion]: { | ||
[Route in keyof EndpointsByVersion[Version]]: "request" extends keyof EndpointsByVersion[Version][Route] | ||
? EndpointsByVersion[Version][Route]["request"] & { | ||
headers: SetRequired<Octokit.RequestHeaders, "accept" | "user-agent">; | ||
} | ||
: never; | ||
}; | ||
}; | ||
// helpers | ||
/** | ||
* Generic type to remap | ||
* | ||
* ```ts | ||
* { k1: { k2: v }} | ||
* ``` | ||
* | ||
* ```ts | ||
* { k2: k1[]} | ||
* ``` | ||
*/ | ||
type Remap<T extends EndpointsByVersion> = { | ||
[P in keyof T as keyof T[P]]: P; | ||
}; | ||
/** | ||
* Generic to find out if an object type has any required keys | ||
*/ | ||
type NonOptionalKeys<Obj> = { | ||
[K in keyof Obj]: {} extends Pick<Obj, K> ? undefined : K; | ||
}[keyof Obj]; | ||
type ArgumentsTypesForRoute< | ||
Route extends string, | ||
Parameters extends Record<string, unknown> | ||
> = NonOptionalKeys<Parameters> extends undefined | ||
? [Route, Parameters?] | ||
: [Route, Parameters]; | ||
/** | ||
* 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()` | ||
*/ | ||
DEFAULTS: TDefaults; | ||
} |
@@ -7,3 +7,3 @@ { | ||
"type": "module", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "Shared TypeScript definitions for upcoming Octokit SDK", | ||
@@ -10,0 +10,0 @@ "types": "./index.d.ts", |
140
request.d.ts
import { Octokit } from "./index.js"; | ||
import { | ||
ArgumentsTypesForRoute, | ||
VersionsByRoute, | ||
AllKnownRoutes, | ||
ParametersByVersionAndRoute, | ||
ResponseByVersionAndRoute, | ||
} from "./utils"; | ||
type UnknownResponse = { | ||
@@ -101,133 +109,1 @@ /** | ||
} | ||
/** | ||
* optimized type to lookup all known routes and the versions they are supported in. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* // import REST API types for github.com, GHES 3.2 and GHES 3.1 | ||
* import "@octokit-next/types-rest-api-ghes-3.1"; | ||
* ``` | ||
* | ||
* The `Octokit.ApiVersions` interface is now looking like this (simplified) | ||
* | ||
* ```ts | ||
* { | ||
* "github.com": { "GET /": { … } }, | ||
* "ghes-3.1": { "GET /": { … }, "GET /admin/tokens": { … } }, | ||
* "ghes-3.2": { "GET /": { … }, "GET /admin/tokens": { … } } | ||
* } | ||
* ``` | ||
* | ||
* The `VersionsByRoute` as a result looks like this | ||
* | ||
* ```ts | ||
* { | ||
* "GET /": "github.com" | "ghes-3.1" | "ghes-3.2", | ||
* "GET /admin/tokens": "ghes-3.1" | "ghes-3.2" | ||
* } | ||
* ``` | ||
*/ | ||
type VersionsByRoute = Remap<EndpointsByVersion>; | ||
// types to improve performance of type lookups | ||
/** | ||
* All known routes across all defined API versions for fast lookup | ||
*/ | ||
type AllKnownRoutes = keyof VersionsByRoute; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { Endpoints: { [route]: Endpoint } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Endpoint } } | ||
* ``` | ||
*/ | ||
type EndpointsByVersion = { | ||
[Version in keyof Octokit.ApiVersions]: "Endpoints" extends keyof Octokit.ApiVersions[Version] | ||
? Octokit.ApiVersions[Version]["Endpoints"] | ||
: never; | ||
}; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { [route]: { parameters: Parameters } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Parameters } } | ||
* ``` | ||
*/ | ||
type ParametersByVersionAndRoute = { | ||
[Version in keyof EndpointsByVersion]: { | ||
[Route in keyof EndpointsByVersion[Version]]: "parameters" extends keyof EndpointsByVersion[Version][Route] | ||
? EndpointsByVersion[Version][Route]["parameters"] & { | ||
headers?: Octokit.RequestHeaders; | ||
} | ||
: never; | ||
}; | ||
}; | ||
/** | ||
* turn | ||
* | ||
* ```ts | ||
* { [version]: { [route]: { response: Response } } } | ||
* ``` | ||
* | ||
* into | ||
* | ||
* ```ts | ||
* { [version]: { [route]: Response } } | ||
* ``` | ||
*/ | ||
type ResponseByVersionAndRoute = { | ||
[Version in keyof EndpointsByVersion]: { | ||
[Route in keyof EndpointsByVersion[Version]]: "response" extends keyof EndpointsByVersion[Version][Route] | ||
? EndpointsByVersion[Version][Route]["response"] | ||
: never; | ||
}; | ||
}; | ||
// helpers | ||
/** | ||
* Generic type to remap | ||
* | ||
* ```ts | ||
* { k1: { k2: v }} | ||
* ``` | ||
* | ||
* ```ts | ||
* { k2: k1[]} | ||
* ``` | ||
*/ | ||
type Remap<T extends EndpointsByVersion> = { | ||
[P in keyof T as keyof T[P]]: P; | ||
}; | ||
/** | ||
* Generic to find out if an object type has any required keys | ||
*/ | ||
type NonOptionalKeys<Obj> = { | ||
[K in keyof Obj]: {} extends Pick<Obj, K> ? undefined : K; | ||
}[keyof Obj]; | ||
type ArgumentsTypesForRoute< | ||
Route extends string, | ||
Parameters extends Record<string, unknown> | ||
> = NonOptionalKeys<Parameters> extends undefined | ||
? [Route, Parameters?] | ||
: [Route, Parameters]; |
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
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
6
27594
823