Skip to content

Commit

Permalink
force to provide param, even undefined, to enable ts-check
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Jan 27, 2024
1 parent cac6b2c commit 2e2801d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
82 changes: 44 additions & 38 deletions src/client.test.ts
Expand Up @@ -16,8 +16,8 @@ Deno.test('client', async () => {
result: 'bar',
}).toString()
)
assertEquals(await client.request('foo'), 'bar')
assertEquals(await client.notify('foo'), undefined)
assertEquals(await client.request('foo', undefined), 'bar')
assertEquals(await client.notify('foo', undefined), undefined)
})

Deno.test('client/batch', async () => {
Expand All @@ -31,14 +31,14 @@ Deno.test('client/batch', async () => {
)
)

assertEquals(await client.batch(client.createRequest('foo')), [{
assertEquals(await client.batch(client.createRequest('foo', undefined)), [{
status: 'fulfilled',
value: 'bar',
}])

assertEquals(
await client.batch(
client.createRequest('foo'),
client.createRequest('foo', undefined),
),
[{
status: 'fulfilled',
Expand All @@ -48,9 +48,9 @@ Deno.test('client/batch', async () => {

assertEquals(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
),
[{
status: 'fulfilled',
Expand All @@ -66,16 +66,16 @@ Deno.test('client/batch', async () => {

assertEquals(
await client.batch(
client.createNotification('foo'),
client.createNotification('foo', undefined),
),
[],
)

assertEquals(
await client.batch(
client.createNotification('foo1'),
client.createNotification('foo2'),
client.createNotification('foo3'),
client.createNotification('foo1', undefined),
client.createNotification('foo2', undefined),
client.createNotification('foo3', undefined),
),
[],
)
Expand All @@ -92,9 +92,9 @@ Deno.test('client/batch', async () => {

assertInstanceOf(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
).catch((e) => e),
JSONRPCError,
)
Expand All @@ -115,9 +115,9 @@ Deno.test('client/batch', async () => {

assertEquals(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
),
[{
status: 'rejected',
Expand Down Expand Up @@ -150,22 +150,22 @@ Deno.test('client/JSONRPCClientParseError', async () => {
client = new JSONRPCClient(() => `malformed json`)

assertInstanceOf(
await client.request('foo').catch((
await client.request('foo', undefined).catch((
e,
) => e),
JSONRPCClientParseError,
)

assertInstanceOf(
await client.batch(client.createRequest('foo')).catch((
await client.batch(client.createRequest('foo', undefined)).catch((
e,
) => e),
JSONRPCClientParseError,
)

assertInstanceOf(
await new JSONRPCClient(() => `{"incorrect": "response object"}`)
.request('foo').catch((
.request('foo', undefined).catch((
e,
) => e),
JSONRPCClientParseError,
Expand All @@ -178,7 +178,7 @@ Deno.test('client/JSONRPCClientParseError', async () => {
result: 6,
})}`
)
.request('foo').catch((
.request('foo', undefined).catch((
e,
) => e),
JSONRPCClientParseError,
Expand All @@ -195,16 +195,16 @@ Deno.test('client/JSONRPCClientParseError', async () => {

assertInstanceOf(
await client.batch(
client.createRequest('foo'),
client.createRequest('foo', undefined),
).catch((e) => e),
JSONRPCClientParseError,
)

assertInstanceOf(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
).catch((e) => e),
JSONRPCClientParseError,
)
Expand All @@ -217,9 +217,9 @@ Deno.test('client/JSONRPCClientParseError', async () => {

assertInstanceOf(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
).catch((e) => e),
JSONRPCClientParseError,
)
Expand All @@ -239,9 +239,9 @@ Deno.test('client/JSONRPCClientParseError', async () => {

assertEquals(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
).catch((e) => e.message),
'ERR_MSG',
)
Expand All @@ -258,9 +258,9 @@ Deno.test('client/JSONRPCClientParseError', async () => {

assertInstanceOf(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
client.createRequest('foo1', undefined),
client.createRequest('foo2', undefined),
client.createRequest('foo3', undefined),
).catch((e) => e),
JSONRPCClientParseError,
)
Expand Down Expand Up @@ -288,13 +288,19 @@ Deno.test({
}).then((res) => res.text())
)

assertInstanceOf(await client.request('system.listMethods'), Array)
assertInstanceOf(
await client.request('system.listMethods', undefined),
Array,
)

assertEquals(await client.notify('system.listMethods'), undefined)
assertEquals(
await client.notify('system.listMethods', undefined),
undefined,
)

const [r1, r2] = await client.batch(
client.createRequest('system.listMethods'),
client.createRequest('system.listMethods'),
client.createRequest('system.listMethods', undefined),
client.createRequest('system.listMethods', undefined),
) as JSONRPCFulfilledResult[]

assertObjectMatch(r1, { status: 'fulfilled' })
Expand Down
8 changes: 4 additions & 4 deletions src/client.ts
Expand Up @@ -51,7 +51,7 @@ export class JSONRPCClient<

public createRequest<T extends keyof Methods>(
method: T extends string ? T : never,
params?: Parameters<Methods[T]>[0],
params: Parameters<Methods[T]>[0],
): JSONRPCRequest {
const id = getIDFromGenerator(this.idGenerator)
const request = new JSONRPCRequest({
Expand All @@ -64,7 +64,7 @@ export class JSONRPCClient<

public createNotification<T extends keyof Methods>(
method: T extends string ? T : never,
params?: Parameters<Methods[T]>[0],
params: Parameters<Methods[T]>[0],
): JSONRPCNotification {
const notification = new JSONRPCNotification({
method,
Expand All @@ -78,7 +78,7 @@ export class JSONRPCClient<
*/
public async request<T extends keyof Methods>(
method: T extends string ? T : never,
params?: Parameters<Methods[T]>[0],
params: Parameters<Methods[T]>[0],
): Promise<ReturnType<Methods[T]>> {
const request = this.createRequest(method, params)
// responded json string
Expand Down Expand Up @@ -119,7 +119,7 @@ export class JSONRPCClient<
*/
public async notify<T extends keyof Methods>(
method: T extends string ? T : never,
params?: Parameters<Methods[T]>[0],
params: Parameters<Methods[T]>[0],
): Promise<void> {
const notification = this.createNotification(method, params)
await this.requestForResponse(JSON.stringify(notification))
Expand Down
1 change: 1 addition & 0 deletions src/index.test.ts
Expand Up @@ -34,6 +34,7 @@ Deno.test('JSONRPCClient/JSONRPCServer', async () => {
await client.request(
// deno-lint-ignore no-explicit-any
'no_such_method' as any,
undefined,
).catch((e) => e),
{
code: -32601,
Expand Down

0 comments on commit 2e2801d

Please sign in to comment.