Skip to content

Commit

Permalink
[Core] Cleanup types, remove unused types and enable marking a Resour…
Browse files Browse the repository at this point in the history
…ce as protected
  • Loading branch information
Pewtro committed Mar 29, 2024
1 parent 5f19abc commit 7388a3b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-clocks-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blizzard-api/core': patch
---

Cleanup types, remove unused types and enable marking a Resource as protected
4 changes: 2 additions & 2 deletions packages/core/src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ interface Endpoints {
tw: Endpoint<'tw'>;
}

export const endpoints: Endpoints = {
const endpoints: Endpoints = {
us: {
hostname: 'https://us.api.blizzard.com',
defaultLocale: 'en_US',
Expand All @@ -58,7 +58,7 @@ export const endpoints: Endpoints = {
},
};

export interface GetEndpoint<T extends Origins> {
interface GetEndpoint<T extends Origins> {
origin: Origins;
locale: (typeof locales)[T][number];
hostname: Endpoint<Origins>['hostname'];
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ export { getEndpoint } from './endpoint.js';
//Namespace
export type { BlizzardNamespaces } from './namespace.js';

//Request
export type { ClientOptions } from './request.js';

//Resource
export type { Resource, ResourceResponse, ResourceOptions, ProtectedResourceOptions } from './resource.js';
export type { Resource, ResourceResponse } from './resource.js';
9 changes: 0 additions & 9 deletions packages/core/src/request.ts

This file was deleted.

30 changes: 24 additions & 6 deletions packages/core/src/resource.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import type { BlizzardNamespaces } from './namespace';
import type { ClientOptions } from './request';

export interface Resource<_Response = void> {
/**
* Represents a resource that can be requested from the Blizzard API
* @param _Response The response type of the resource
* @param ProtectedResource Whether the resource requires a token to be requested
*/
export type Resource<_Response, ProtectedResource extends boolean = false> = {
path: string;
namespace?: BlizzardNamespaces;
}
export type ResourceResponse<T = unknown> = Promise<T>;
} & (ProtectedResource extends true ? { token: string } : unknown);

export type ResourceOptions<T = unknown> = Partial<ClientOptions> & T;
/**
* Represents the response of a resource
* @param T The response type of the resource
* @example
* type response = ResourceResponse<{ id: number }>;
* const response: response = Promise.resolve({ id: 1 });
* response.then((data) => console.log(data.id));
*/
export type ResourceResponse<T = unknown> = Promise<T>;

export type ProtectedResourceOptions<T = unknown> = Partial<ClientOptions> & { token: string } & T;
/**
* Extracts the response type from a resource
* @param Type The resource type
* @returns The response type
* @example
* type extracted = ExtractResourceType<Resource<{ id: number }>>;
*/
export type ExtractResourceType<Type> = Type extends Resource<infer R> ? R : never;

0 comments on commit 7388a3b

Please sign in to comment.