Skip to content

Commit

Permalink
fix: unified the request that the types contain request body to be `u…
Browse files Browse the repository at this point in the history
…ndefined`
  • Loading branch information
JOU-amjs committed Sep 27, 2024
1 parent 79c2263 commit 0c7469c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-balloons-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'alova': patch
---

unified the request that the types contain request body to be `undefined`
8 changes: 4 additions & 4 deletions packages/alova/src/alova.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class Alova<AG extends AlovaGenerics> {

Post<Responded = unknown, Transformed = unknown>(
url: string,
data: RequestBody = {},
data?: RequestBody,
config?: AlovaMethodCreateConfig<AG, Responded, Transformed>
) {
return newInstance(
Expand All @@ -97,7 +97,7 @@ export class Alova<AG extends AlovaGenerics> {

Delete<Responded = unknown, Transformed = unknown>(
url: string,
data: RequestBody = {},
data?: RequestBody,
config?: AlovaMethodCreateConfig<AG, Responded, Transformed>
) {
return newInstance(
Expand All @@ -112,7 +112,7 @@ export class Alova<AG extends AlovaGenerics> {

Put<Responded = unknown, Transformed = unknown>(
url: string,
data: RequestBody = {},
data?: RequestBody,
config?: AlovaMethodCreateConfig<AG, Responded, Transformed>
) {
return newInstance(
Expand All @@ -134,7 +134,7 @@ export class Alova<AG extends AlovaGenerics> {

Patch<Responded = unknown, Transformed = unknown>(
url: string,
data: RequestBody = {},
data?: RequestBody,
config?: AlovaMethodCreateConfig<AG, Responded, Transformed>
) {
return newInstance(
Expand Down
10 changes: 5 additions & 5 deletions packages/alova/test/browser/behavior/l1Cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('l1cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});

// the `cacheFor` also set to an object
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('l1cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
});

Expand Down Expand Up @@ -102,7 +102,7 @@ describe('l1cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});

// the `cacheFor` also set to an object
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('l1cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
});

Expand Down Expand Up @@ -167,7 +167,7 @@ describe('l1cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
expect(expireFn).toHaveBeenCalledWith(Post, 'memory');
});
Expand Down
8 changes: 4 additions & 4 deletions packages/alova/test/browser/behavior/l2Cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ describe('l2cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
expect(await queryCache(Post1, { policy: 'l2' })).toStrictEqual({
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});

// POST is cached
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('l2cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
expect(expireFn).toHaveBeenCalledTimes(2);
});
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('l2cache cache data', () => {
path: '/unit-test',
method: 'POST',
params: {},
data: {}
data: ''
});
expect(expireFn).toHaveBeenCalledTimes(2);
});
Expand Down
26 changes: 26 additions & 0 deletions packages/alova/test/browser/global/methodInstance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,32 @@ describe('method instance', () => {
expect(res.params).toStrictEqual({ a: 'a', b: 'str' });
});

test('should the request body will be `undefined` initially', async () => {
const methodGet = alova.Get('/unit-test');
const methodPost = alova.Post('/unit-test');
const methodHead = alova.Head('/unit-test');
const methodPut = alova.Put('/unit-test');
const methodDelete = alova.Delete('/unit-test');
const methodPatch = alova.Patch('/unit-test');
const methodOptions = alova.Options('/unit-test');
expect(methodGet.data).toBeUndefined();
expect(methodPost.data).toBeUndefined();
expect(methodHead.data).toBeUndefined();
expect(methodPut.data).toBeUndefined();
expect(methodDelete.data).toBeUndefined();
expect(methodPatch.data).toBeUndefined();
expect(methodOptions.data).toBeUndefined();

const methodPost2 = alova.Post('/unit-test', {});
const methodPut2 = alova.Put('/unit-test', {});
const methodDelete2 = alova.Delete('/unit-test', {});
const methodPatch2 = alova.Patch('/unit-test', {});
expect(methodPost2.data).toStrictEqual({});
expect(methodPut2.data).toStrictEqual({});
expect(methodDelete2.data).toStrictEqual({});
expect(methodPatch2.data).toStrictEqual({});
});

test('it can customize the method key', async () => {
const method1 = alova.Get('/unit-test');
method1.key = 'method1-key';
Expand Down

0 comments on commit 0c7469c

Please sign in to comment.