Skip to content

Commit

Permalink
Export additional useful types (#137)
Browse files Browse the repository at this point in the history
* Create additional `XyzRequest` types for consistency
* Export them and other useful types
  • Loading branch information
trevor-scheer authored Jan 12, 2023
1 parent 9220713 commit c9ffa7f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-sheep-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@apollo/datasource-rest': patch
---

Create intermediate request types (`PostRequest`, etc.) for consistency and export them.
Export `DataSourceRequest`, `DataSourceConfig`, and `DataSourceFetchResult` types.
39 changes: 30 additions & 9 deletions src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,35 @@ export interface GetRequest extends RequestOptions {
body?: never;
}

export type RequestWithoutBody = HeadRequest | GetRequest;

export interface RequestWithBody extends Omit<RequestOptions, 'body'> {
method?: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
interface WithBody extends Omit<RequestOptions, 'body'> {
body?: FetcherRequestInit['body'] | object;
}

type DataSourceRequest = RequestWithoutBody | RequestWithBody;
export interface PostRequest extends WithBody {
method?: 'POST';
}

export interface PutRequest extends WithBody {
method?: 'PUT';
}

export interface PatchRequest extends WithBody {
method?: 'PATCH';
}

export interface DeleteRequest extends WithBody {
method?: 'DELETE';
}

export type RequestWithoutBody = HeadRequest | GetRequest;

export type RequestWithBody =
| PostRequest
| PutRequest
| PatchRequest
| DeleteRequest;

export type DataSourceRequest = RequestWithoutBody | RequestWithBody;

// While tempting, this union can't be reduced / factored out to just
// Omit<WithRequired<RequestWithBody | RequestWithBody, 'headers'>, 'params'> & { params: URLSearchParams }
Expand Down Expand Up @@ -366,7 +387,7 @@ export abstract class RESTDataSource {

protected async post<TResult = any>(
path: string,
request?: RequestWithBody,
request?: PostRequest,
): Promise<TResult> {
return (
await this.fetch<TResult>(path, {
Expand All @@ -378,7 +399,7 @@ export abstract class RESTDataSource {

protected async patch<TResult = any>(
path: string,
request?: RequestWithBody,
request?: PatchRequest,
): Promise<TResult> {
return (
await this.fetch<TResult>(path, {
Expand All @@ -390,7 +411,7 @@ export abstract class RESTDataSource {

protected async put<TResult = any>(
path: string,
request?: RequestWithBody,
request?: PutRequest,
): Promise<TResult> {
return (
await this.fetch<TResult>(path, {
Expand All @@ -402,7 +423,7 @@ export abstract class RESTDataSource {

protected async delete<TResult = any>(
path: string,
request?: RequestWithBody,
request?: DeleteRequest,
): Promise<TResult> {
return (
await this.fetch<TResult>(path, {
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@ export {
RESTDataSource,
RequestOptions,
AugmentedRequest,
DataSourceFetchResult,
DataSourceConfig,
GetRequest,
PatchRequest,
PostRequest,
PutRequest,
DeleteRequest,
HeadRequest,
RequestWithoutBody,
RequestWithBody,
DataSourceRequest,
} from './RESTDataSource';

0 comments on commit c9ffa7f

Please sign in to comment.