From 0d0900b6d8201eaf8cb9b8c27e90f70be4a71c84 Mon Sep 17 00:00:00 2001 From: Jack Works Date: Sun, 11 Feb 2024 20:28:30 +0800 Subject: [PATCH] refactor: rename "key" to "name" --- .changeset/lemon-bikes-bake.md | 5 ++++ .../__file_snapshots__/async-call-key-name.md | 1 + __tests__/options.ts | 11 +++++++++ api/base.api.md | 2 ++ api/full.api.md | 2 ++ docs/async-call-rpc.asynccalloptions.key.md | 5 ++++ docs/async-call-rpc.asynccalloptions.md | 1 + docs/async-call-rpc.asynccalloptions.name.md | 13 +++++++++++ src/Async-Call.ts | 23 +++++++++++-------- src/types.ts | 6 +++++ 10 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 .changeset/lemon-bikes-bake.md create mode 100644 __tests__/__file_snapshots__/async-call-key-name.md create mode 100644 docs/async-call-rpc.asynccalloptions.name.md diff --git a/.changeset/lemon-bikes-bake.md b/.changeset/lemon-bikes-bake.md new file mode 100644 index 0000000..cb7bf50 --- /dev/null +++ b/.changeset/lemon-bikes-bake.md @@ -0,0 +1,5 @@ +--- +"async-call-rpc": minor +--- + +rename "key" to "name" diff --git a/__tests__/__file_snapshots__/async-call-key-name.md b/__tests__/__file_snapshots__/async-call-key-name.md new file mode 100644 index 0000000..0ad65f9 --- /dev/null +++ b/__tests__/__file_snapshots__/async-call-key-name.md @@ -0,0 +1 @@ +# Timeline diff --git a/__tests__/options.ts b/__tests__/options.ts index 5a0b21a..f456cb2 100644 --- a/__tests__/options.ts +++ b/__tests__/options.ts @@ -108,3 +108,14 @@ it( expect(a).toHaveProperty('then', undefined) }), ) + +it( + 'should error if both name and key are provided', + withSnapshotDefault('async-call-key-name', async ({ init }) => { + expect(() => + init({ + options: { key: 'a', name: 'b' }, + }), + ).toThrowErrorMatchingInlineSnapshot(`[TypeError: Please remove key.]`) + }), +) diff --git a/api/base.api.md b/api/base.api.md index ef8815e..d5a0814 100644 --- a/api/base.api.md +++ b/api/base.api.md @@ -22,10 +22,12 @@ export interface AsyncCallOptions | EventBasedChannel | Promise | EventBasedChannel>; encoder?: IsomorphicEncoder | IsomorphicEncoderFull; idGenerator?(): string | number; + // @deprecated key?: string; log?: AsyncCallLogLevel | boolean | 'all'; logger?: ConsoleInterface; mapError?: ErrorMapFunction; + name?: string; parameterStructures?: 'by-position' | 'by-name'; preferLocalImplementation?: boolean; // @deprecated diff --git a/api/full.api.md b/api/full.api.md index a610b82..ec4f424 100644 --- a/api/full.api.md +++ b/api/full.api.md @@ -22,10 +22,12 @@ export interface AsyncCallOptions | EventBasedChannel | Promise | EventBasedChannel>; encoder?: IsomorphicEncoder | IsomorphicEncoderFull; idGenerator?(): string | number; + // @deprecated key?: string; log?: AsyncCallLogLevel | boolean | 'all'; logger?: ConsoleInterface; mapError?: ErrorMapFunction; + name?: string; parameterStructures?: 'by-position' | 'by-name'; preferLocalImplementation?: boolean; // @deprecated diff --git a/docs/async-call-rpc.asynccalloptions.key.md b/docs/async-call-rpc.asynccalloptions.key.md index ae46936..ed90ea5 100644 --- a/docs/async-call-rpc.asynccalloptions.key.md +++ b/docs/async-call-rpc.asynccalloptions.key.md @@ -4,6 +4,11 @@ ## AsyncCallOptions.key property +> Warning: This API is now obsolete. +> +> Renamed to "name". +> + Name used when pretty log is enabled. **Signature:** diff --git a/docs/async-call-rpc.asynccalloptions.md b/docs/async-call-rpc.asynccalloptions.md index eebe4cb..5e3a1c0 100644 --- a/docs/async-call-rpc.asynccalloptions.md +++ b/docs/async-call-rpc.asynccalloptions.md @@ -22,6 +22,7 @@ export interface AsyncCallOptions<unknown> | _(Optional)_ Change the [ErrorResponseDetail](./async-call-rpc.errorresponsedetail.md). | +| [name?](./async-call-rpc.asynccalloptions.name.md) | | string | _(Optional)_ Name used when pretty log is enabled. | | [parameterStructures?](./async-call-rpc.asynccalloptions.parameterstructures.md) | | 'by-position' \| 'by-name' | _(Optional)_ Choose flavor of parameter structures defined in the spec | | [preferLocalImplementation?](./async-call-rpc.asynccalloptions.preferlocalimplementation.md) | | boolean | _(Optional)_ Prefer local implementation than remote. | | [serializer?](./async-call-rpc.asynccalloptions.serializer.md) | | [Serialization](./async-call-rpc.serialization.md) | _(Optional)_ Serializer of the requests and responses. | diff --git a/docs/async-call-rpc.asynccalloptions.name.md b/docs/async-call-rpc.asynccalloptions.name.md new file mode 100644 index 0000000..8c8cd42 --- /dev/null +++ b/docs/async-call-rpc.asynccalloptions.name.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [async-call-rpc](./async-call-rpc.md) > [AsyncCallOptions](./async-call-rpc.asynccalloptions.md) > [name](./async-call-rpc.asynccalloptions.name.md) + +## AsyncCallOptions.name property + +Name used when pretty log is enabled. + +**Signature:** + +```typescript +name?: string; +``` diff --git a/src/Async-Call.ts b/src/Async-Call.ts index c67d887..4b44e45 100644 --- a/src/Async-Call.ts +++ b/src/Async-Call.ts @@ -112,7 +112,8 @@ export function AsyncCall( const { serializer, encoder, - key: logKey = 'rpc', + key: deprecatedName, + name, strict = true, log = true, parameterStructures = 'by-position', @@ -124,6 +125,11 @@ export function AsyncCall( thenable, } = options + // Note: we're not shorten this error message because it will be removed in the next major version. + if (serializer && encoder) throw new TypeError('Please remove serializer.') + if (name && deprecatedName) throw new TypeError('Please remove key.') + const logKey = name || deprecatedName || 'rpc' + const { encode: encodeFromOption, encodeRequest: encodeRequestFromOption, @@ -136,8 +142,8 @@ export function AsyncCall( const encodeRequest: (data: Requests | Responses) => any = encoder ? (data) => apply(encodeRequestFromOption || encodeFromOption, encoder, [data]) : serializer - ? (data) => serializer.serialization(data) - : Object + ? (data) => serializer.serialization(data) + : Object const encodeResponse: (data: Requests | Responses) => any = encoder ? (data) => apply(encodeResponseFromOption || encodeFromOption, encoder, [data]) @@ -150,14 +156,11 @@ export function AsyncCall( hint == 'request' ? apply(decodeRequest || decode, encoder, [data]) : hint == 'response' - ? apply(decodeResponse || decode, encoder, [data]) - : apply(decode, encoder, [data]) + ? apply(decodeResponse || decode, encoder, [data]) + : apply(decode, encoder, [data]) : serializer - ? (data) => serializer.deserialization(data) - : Object - - // Note: we're not shorten this error message because it will be removed in the next major version. - if (serializer && encoder) throw new TypeError('Please remove serializer.') + ? (data) => serializer.deserialization(data) + : Object if (thisSideImplementation instanceof Promise) awaitThisSideImplementation() else { diff --git a/src/types.ts b/src/types.ts index 352f308..2be5b37 100644 --- a/src/types.ts +++ b/src/types.ts @@ -126,10 +126,16 @@ export interface AsyncCallOptions