Skip to content

Commit

Permalink
refactor: rename "key" to "name"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Feb 11, 2024
1 parent 0e661c6 commit 0d0900b
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-bikes-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"async-call-rpc": minor
---

rename "key" to "name"
1 change: 1 addition & 0 deletions __tests__/__file_snapshots__/async-call-key-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Timeline
11 changes: 11 additions & 0 deletions __tests__/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.]`)
}),
)
2 changes: 2 additions & 0 deletions api/base.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export interface AsyncCallOptions<EncodedRequest = unknown, EncodedResponse = un
channel: CallbackBasedChannel<EncodedRequest | EncodedResponse> | EventBasedChannel<EncodedRequest | EncodedResponse> | Promise<CallbackBasedChannel<EncodedRequest | EncodedResponse> | EventBasedChannel<EncodedRequest | EncodedResponse>>;
encoder?: IsomorphicEncoder<EncodedRequest, EncodedResponse> | IsomorphicEncoderFull<EncodedRequest, EncodedResponse>;
idGenerator?(): string | number;
// @deprecated
key?: string;
log?: AsyncCallLogLevel | boolean | 'all';
logger?: ConsoleInterface;
mapError?: ErrorMapFunction<unknown>;
name?: string;
parameterStructures?: 'by-position' | 'by-name';
preferLocalImplementation?: boolean;
// @deprecated
Expand Down
2 changes: 2 additions & 0 deletions api/full.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export interface AsyncCallOptions<EncodedRequest = unknown, EncodedResponse = un
channel: CallbackBasedChannel<EncodedRequest | EncodedResponse> | EventBasedChannel<EncodedRequest | EncodedResponse> | Promise<CallbackBasedChannel<EncodedRequest | EncodedResponse> | EventBasedChannel<EncodedRequest | EncodedResponse>>;
encoder?: IsomorphicEncoder<EncodedRequest, EncodedResponse> | IsomorphicEncoderFull<EncodedRequest, EncodedResponse>;
idGenerator?(): string | number;
// @deprecated
key?: string;
log?: AsyncCallLogLevel | boolean | 'all';
logger?: ConsoleInterface;
mapError?: ErrorMapFunction<unknown>;
name?: string;
parameterStructures?: 'by-position' | 'by-name';
preferLocalImplementation?: boolean;
// @deprecated
Expand Down
5 changes: 5 additions & 0 deletions docs/async-call-rpc.asynccalloptions.key.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

## AsyncCallOptions.key property

> Warning: This API is now obsolete.
>
> Renamed to "name".
>
Name used when pretty log is enabled.

**Signature:**
Expand Down
1 change: 1 addition & 0 deletions docs/async-call-rpc.asynccalloptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface AsyncCallOptions<EncodedRequest = unknown, EncodedResponse = un
| [log?](./async-call-rpc.asynccalloptions.log.md) | | [AsyncCallLogLevel](./async-call-rpc.asynccallloglevel.md) \| boolean \| 'all' | _(Optional)_ Choose log level. |
| [logger?](./async-call-rpc.asynccalloptions.logger.md) | | [ConsoleInterface](./async-call-rpc.consoleinterface.md) | _(Optional)_ Provide the logger |
| [mapError?](./async-call-rpc.asynccalloptions.maperror.md) | | [ErrorMapFunction](./async-call-rpc.errormapfunction.md)<!-- -->&lt;unknown&gt; | _(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. |
Expand Down
13 changes: 13 additions & 0 deletions docs/async-call-rpc.asynccalloptions.name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [async-call-rpc](./async-call-rpc.md) &gt; [AsyncCallOptions](./async-call-rpc.asynccalloptions.md) &gt; [name](./async-call-rpc.asynccalloptions.name.md)

## AsyncCallOptions.name property

Name used when pretty log is enabled.

**Signature:**

```typescript
name?: string;
```
23 changes: 13 additions & 10 deletions src/Async-Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
const {
serializer,
encoder,
key: logKey = 'rpc',
key: deprecatedName,
name,
strict = true,
log = true,
parameterStructures = 'by-position',
Expand All @@ -124,6 +125,11 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
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,
Expand All @@ -136,8 +142,8 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
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])
Expand All @@ -150,14 +156,11 @@ export function AsyncCall<OtherSideImplementedFunctions = {}>(
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 {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ export interface AsyncCallOptions<EncodedRequest = unknown, EncodedResponse = un
/**
* Name used when pretty log is enabled.
* @defaultValue `rpc`
* @deprecated Renamed to "name".
* @privateRemarks
* TODO: rename this option to name.
*/
key?: string
/**
* Name used when pretty log is enabled.
* @defaultValue `rpc`
*/
name?: string
/**
* Serializer of the requests and responses.
* @deprecated Use "encoding" option instead. This option will be removed in the next major version.
Expand Down

0 comments on commit 0d0900b

Please sign in to comment.