Skip to content

Commit f674a39

Browse files
authored
Merge pull request #2 from WJSoftware:JP/DrFetch
chore!: Rename WjFetch to DrFetch
2 parents 6bfb367 + 301a54d commit f674a39

File tree

6 files changed

+36
-33
lines changed

6 files changed

+36
-33
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,28 @@ Think of this custom function as the place where you do interceptions (if you ar
5555
### Create Fetcher Object
5656

5757
```typescript
58-
import { WjFetch } from "dr-fetch";
58+
import { DrFetch } from "dr-fetch";
5959
import { myFetch } from "./myFetch.js";
6060

61-
const fetcher = new WjFetch(myFetch);
61+
const fetcher = new DrFetch(myFetch);
6262
// If you don't need a custom fetch function, just do:
63-
const fetcher = new WjFetch();
63+
const fetcher = new DrFetch();
6464
```
6565

6666
### Adding a Custom Body Parser
6767

68-
One can say that the `WjFetch` class comes with 2 basic body parsers:
68+
One can say that the `DrFetch` class comes with 2 basic body parsers:
6969

7070
1. JSON parser when the the value of the `coontent-type` response header is `application/json` or similar
7171
(`application/problem+json`, for instance).
7272
2. Text parser when the vlaue of the `content-type` response header is `text/<something>`, such as `text/plain` or
7373
`text/csv`.
7474

75-
If your API sends a content type not included in any of the above two cases, use `WjFetch.withParser()` to add a custom
75+
If your API sends a content type not included in any of the above two cases, use `DrFetch.withParser()` to add a custom
7676
parser for the content type you are expecting. The class allows for fluent syntax, so you can chain calls:
7777

7878
```typescript
79-
const fetcher = new WjFetch(myFetch)
79+
const fetcher = new DrFetch(myFetch)
8080
.withParser('custom/contentType', async (response) => {
8181
// Do what you must with the provided response object. In the end, you must return the parsed body.
8282
return finalBody;
@@ -131,11 +131,11 @@ what if your API is standardized so all status `400` bodies look the same? Then
131131

132132
```typescript
133133
// root-fetcher.ts
134-
import { WjFetch } from "dr-fetch";
134+
import { DrFetch } from "dr-fetch";
135135
import { myFetch } from "./my-fetch.js";
136136
import type { BadRequestBody } from "my-datatypes.js";
137137

138-
export default new WjFetch(myFetch)
138+
export default new DrFetch(myFetch)
139139
.withParser(...) // Optional parsers
140140
.withParser(...)
141141
.for<400, BadRequestBody>()

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"type": "module",
88
"scripts": {
99
"test": "ts-mocha -n loader=ts-node/esm -p ./tsconfig.json ./src/tests/**/*.test.ts",
10-
"build": "npx tsc && npx publint"
10+
"build": "npx tsc && npx publint",
11+
"prebuild": "npm run test",
12+
"publish": "npm publish",
13+
"prepublishonly": "npm run build"
1114
},
1215
"keywords": [
1316
"fetch"

src/WjFetch.ts renamed to src/DrFetch.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const textTypes: (string | RegExp)[] = [
1717
];
1818

1919
/**
20-
* # WjFetch
20+
* # DrFetch
2121
*
2222
* Class that wraps around the provided data-fetching function (or the standard `fetch` function) in order to provide
2323
* full body typing.
@@ -32,7 +32,7 @@ const textTypes: (string | RegExp)[] = [
3232
* type ToDo = { id: number; text: string; }
3333
* type NotAuthBody = { loginUrl: string; }
3434
*
35-
* const fetcher = new WjFetch()
35+
* const fetcher = new DrFetch()
3636
* .for<200, ToDo[]>()
3737
* .for<401, NotAuthBody>()
3838
* ;
@@ -55,7 +55,7 @@ const textTypes: (string | RegExp)[] = [
5555
*
5656
* @example
5757
* ```typescript
58-
* const fetcher = new WjFetch()
58+
* const fetcher = new DrFetch()
5959
* .for<200 | 201, { data: ToDo }>()
6060
* ;
6161
* ```
@@ -66,7 +66,7 @@ const textTypes: (string | RegExp)[] = [
6666
*
6767
* @example
6868
* ```typescript
69-
* const fetcher = new WjFetch()
69+
* const fetcher = new DrFetch()
7070
* .for<OkStatusCode, { data: ToDo }>()
7171
* ;
7272
* ```
@@ -80,7 +80,7 @@ const textTypes: (string | RegExp)[] = [
8080
* existing one using the parent's `clone` function. When cloning, pass a new data-fetching function (if required) so
8181
* the clone uses this one instead of the one of the parent fetcher.
8282
*/
83-
export class WjFetch<T = unknown> {
83+
export class DrFetch<T = unknown> {
8484
#fetchFn: typeof fetch;
8585
#customParsers: [string | RegExp, (response: Response) => Promise<any>][] = [];
8686

@@ -107,7 +107,7 @@ export class WjFetch<T = unknown> {
107107
* }
108108
*
109109
* // Create the class now.
110-
* const fetcher = new WjFetch(myCustomFetch);
110+
* const fetcher = new DrFetch(myCustomFetch);
111111
* ```
112112
*
113113
* If you need to do special parsing of the body, don't do post-interception and instead use the `withParser`
@@ -134,12 +134,12 @@ export class WjFetch<T = unknown> {
134134
* Determines if parsers are included in the clone. The default is `true`.
135135
*/
136136
includeParsers?: boolean;
137-
}): TInherit extends true ? WjFetch<T> : WjFetch {
138-
const newClone = new WjFetch(options?.fetchFn ?? this.#fetchFn);
137+
}): TInherit extends true ? DrFetch<T> : DrFetch {
138+
const newClone = new DrFetch(options?.fetchFn ?? this.#fetchFn);
139139
if (options?.includeParsers ?? true) {
140140
newClone.#customParsers = [...this.#customParsers];
141141
}
142-
return newClone as WjFetch<T>;
142+
return newClone as DrFetch<T>;
143143
}
144144

145145
/**
@@ -163,8 +163,8 @@ export class WjFetch<T = unknown> {
163163
* be a single status code, or multiple status codes.
164164
* @returns This parser object with its response type modified to include the body specification provided.
165165
*/
166-
for<TStatus extends StatusCode, TBody = {}>(): WjFetch<FetchResult<T, TStatus, TBody>> {
167-
return this as WjFetch<FetchResult<T, TStatus, TBody>>;
166+
for<TStatus extends StatusCode, TBody = {}>(): DrFetch<FetchResult<T, TStatus, TBody>> {
167+
return this as DrFetch<FetchResult<T, TStatus, TBody>>;
168168
}
169169

170170
// notFor<TStatus extends StatusCode>(status: TStatus)

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1+
export * from './DrFetch.js';
12
export type * from './types.js';
2-
export * from './WjFetch.js';
33

src/tests/WjFetch.test.ts renamed to src/tests/DrFetch.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect } from "chai";
22
import { describe, test } from "mocha";
33
import { fake } from 'sinon';
4-
import { WjFetch } from "../WjFetch.js";
4+
import { DrFetch } from "../DrFetch.js";
55

6-
describe('WjFetch', () => {
6+
describe('DrFetch', () => {
77
describe('clone()', () => {
88
[
99
{
@@ -32,7 +32,7 @@ describe('WjFetch', () => {
3232
const contentType = 'text/plain';
3333
const origFetchFn = fake.resolves(new Response('Hi!', { headers: { 'content-type': contentType } }));
3434
const customParserFn = fake();
35-
const origFetcher = new WjFetch(origFetchFn);
35+
const origFetcher = new DrFetch(origFetchFn);
3636
origFetcher.withParser(contentType, customParserFn);
3737
const newFetchFn = fake.resolves(new Response('Hi!', { headers: { 'content-type': contentType } }));
3838

@@ -52,7 +52,7 @@ describe('WjFetch', () => {
5252
const origFetch = globalThis.fetch;
5353
const fakeFetch = fake.resolves(new Response(null));
5454
globalThis.fetch = fakeFetch;
55-
const fetcher = new WjFetch();
55+
const fetcher = new DrFetch();
5656

5757
// Act.
5858
try {
@@ -68,7 +68,7 @@ describe('WjFetch', () => {
6868
test("Should call the provided data-fetching function.", async () => {
6969
// Arrange.
7070
const fakeFetch = fake.resolves(new Response(null));
71-
const fetcher = new WjFetch(fakeFetch);
71+
const fetcher = new DrFetch(fakeFetch);
7272

7373
// Act.
7474
await fetcher.fetch('x');
@@ -116,7 +116,7 @@ describe('WjFetch', () => {
116116
test(`Should parse the body using the stock body parsers when content type is "${tc.contentType}".`, async () => {
117117
// Arrange.
118118
const fetchFn = fake.resolves(new Response(tc.body, { headers: { 'content-type': tc.contentType } }));
119-
const fetcher = new WjFetch(fetchFn);
119+
const fetcher = new DrFetch(fetchFn);
120120

121121
// Act.
122122
const result = await fetcher.for<200, string | object>().fetch('x');
@@ -133,7 +133,7 @@ describe('WjFetch', () => {
133133
test("Should return 'null' as body whenever the response carries no body.", async () => {
134134
// Arrange.
135135
const fetchFn = fake.resolves(new Response());
136-
const fetcher = new WjFetch(fetchFn);
136+
const fetcher = new DrFetch(fetchFn);
137137

138138
// Act.
139139
const response = await fetcher.for<200, string>().fetch('x');
@@ -146,7 +146,7 @@ describe('WjFetch', () => {
146146
const response = new Response('x');
147147
response.headers.delete('content-type');
148148
const fetchFn = fake.resolves(response);
149-
const fetcher = new WjFetch(fetchFn);
149+
const fetcher = new DrFetch(fetchFn);
150150
let didThrow = false;
151151

152152
// Act.
@@ -163,7 +163,7 @@ describe('WjFetch', () => {
163163
test("Should throw an error if the content type is unknown by the built-in parsers and custom parsers.", async () => {
164164
// Arrange.
165165
const fetchFn = fake.resolves(new Response('x', { headers: { 'content-type': 'application/xml' }}));
166-
const fetcher = new WjFetch(fetchFn);
166+
const fetcher = new DrFetch(fetchFn);
167167
let didThrow = false;
168168

169169
// Act.
@@ -200,7 +200,7 @@ describe('WjFetch', () => {
200200
],
201201
},
202202
].flatMap(x => {
203-
const expanded = [];
203+
const expanded: (Omit<(typeof x), 'contentTypes'> & { contentType: string; })[] = [];
204204
for (let ct of x.contentTypes) {
205205
expanded.push({
206206
pattern: x.pattern,
@@ -214,7 +214,7 @@ describe('WjFetch', () => {
214214
// Arrange.
215215
const parserFn = fake();
216216
const fetchFn = fake.resolves(new Response('x', { headers: { 'content-type': tc.contentType }}));
217-
const fetcher = new WjFetch(fetchFn);
217+
const fetcher = new DrFetch(fetchFn);
218218
fetcher.withParser(tc.pattern, parserFn);
219219

220220
// Act.

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type CoreFetchResult<TStatus extends StatusCode, TBody> = {
3636
});
3737

3838
/**
39-
* Type that builds WjFetch's final result object's type.
39+
* Type that builds DrFetch's final result object's type.
4040
*/
4141
export type FetchResult<T, TStatus extends StatusCode, TBody = undefined> =
4242
(unknown extends T ? CoreFetchResult<TStatus, TBody> : T | CoreFetchResult<TStatus, TBody>) extends infer R ? R : never;

0 commit comments

Comments
 (0)