Skip to content

Commit

Permalink
rename MethodSet to Methods & 100% cov
Browse files Browse the repository at this point in the history
  • Loading branch information
YieldRay committed Jan 27, 2024
1 parent 6d498ee commit 263bc2c
Show file tree
Hide file tree
Showing 9 changed files with 250 additions and 175 deletions.
95 changes: 29 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,55 @@ A strictly typed json-rpc(2.0) implementation, zero dependency, minimal abstract

> Specification <https://www.jsonrpc.org/specification>
```ts
const methodSet = {
upper: (str: string) => str.toUpperCase(),
lower: (str: string) => str.toLowerCase(),
plus: ([a, b]: [number, number]) => a + b,
minus: ([a, b]: [number, number]) => a - b,
}

// initialize all methods with the constructor
const server = new JSONRPCServer(methodSet)

// or add methods manually
const server = new JSONRPCServer<typeof methodSet>()
server.setMethod('upper', methodSet.upper)
server.setMethod('lower', methodSet.lower)
server.setMethod('plus', methodSet.plus)
server.setMethod('minus', methodSet.minus)

// (optional) provide a generic parameter to enable ts check
const client = new JSONRPCClient<typeof methodSet>((json) =>
server.process(json)
)

// request, Notification and batch are always async
assertEquals(await client.request('upper', 'hello'), 'HELLO')

assertEquals(
await client.batch(
client.createRequest('upper', 'nihao'),
// Notification does not have response, even when response errors
client.createNotification('upper'),
client.createRequest('upper', 'shijie'),
client.createRequest('plus', [1, 1]),
),
[
{
status: 'fulfilled',
value: 'NIHAO',
},
{
status: 'fulfilled',
value: 'SHIJIE',
},
{
status: 'fulfilled',
value: 2,
},
],
)
```

Example to use the client

```ts
const client = new JSONRPCClient((json) =>
const requestForResponse = (json: string) =>
fetch('http://localhost:6800/jsonrpc', {
method: 'POST',
body: json,
}).then((res) => res.text())
)

const aria2cMethods = await client.request('system.listMethods')
const client = new JSONRPCClient<{
'aria2.addUri': (
urls: string[],
options?: object,
position?: number,
) => string
'aria2.remove': (gid: string) => string
'system.listMethods': () => string[]
}>(requestForResponse)

const resultGid: string = await client.request('aria2.addUri', [
['https://example.net/index.html'],
{},
0,
])
```

Example to use the server

```ts
const server = new JSONRPCServer()
const server = new JSONRPCServer({
upper: (str: string) => str.toUpperCase(),
lower: (str: string) => str.toLowerCase(),
plus: ([a, b]: [number, number]) => a + b,
minus: ([a, b]: [number, number]) => a - b,
})

// or:
server.setMethod('trim', (str: string) => str.trim())
server.setMethod('trimStart', (str: string) => str.trimStart())
server.setMethod('trimEnd', (str: string) => str.trimEnd())

const httpServer = Deno.serve(
async (request) => {
const url = new URL(request.url)
if (url.pathname === '/jsonrpc') {
return new Response(
// server.process() accept string and returns Promise<string>
await server.process(await request.text()),
{
headers: { 'content-type': 'application/json' },
},
)
}
return new Response('404', { status: 404 })
// server.handleRequest() accept string and returns Promise<string>
const jsonString = await server.handleRequest(await request.text())

return new Response(jsonString, {
headers: { 'content-type': 'application/json' },
})
},
)
```
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"noImplicitOverride": true
},
"imports": {
"std/": "https://deno.land/std@0.209.0/"
"std/": "https://deno.land/std@0.213.0/"
},
"tasks": {
"lint": "deno lint",
Expand Down
34 changes: 33 additions & 1 deletion deno.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@
"https://deno.land/std@0.209.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085",
"https://deno.land/std@0.209.0/assert/unimplemented.ts": "4e3e504792c87c485dbc5f4020489d8806ef697741403af2008dfa7b5a4711e8",
"https://deno.land/std@0.209.0/assert/unreachable.ts": "1af8c99421cc5fb7332454b2b9eca074a4e394895a180bc837750dedcca75338",
"https://deno.land/std@0.209.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2"
"https://deno.land/std@0.209.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2",
"https://deno.land/std@0.213.0/assert/_constants.ts": "a271e8ef5a573f1df8e822a6eb9d09df064ad66a4390f21b3e31f820a38e0975",
"https://deno.land/std@0.213.0/assert/_diff.ts": "dcc63d94ca289aec80644030cf88ccbf7acaa6fbd7b0f22add93616b36593840",
"https://deno.land/std@0.213.0/assert/_format.ts": "0ba808961bf678437fb486b56405b6fefad2cf87b5809667c781ddee8c32aff4",
"https://deno.land/std@0.213.0/assert/assert.ts": "bec068b2fccdd434c138a555b19a2c2393b71dfaada02b7d568a01541e67cdc5",
"https://deno.land/std@0.213.0/assert/assert_almost_equals.ts": "8b96b7385cc117668b0720115eb6ee73d04c9bcb2f5d2344d674918c9113688f",
"https://deno.land/std@0.213.0/assert/assert_array_includes.ts": "1688d76317fd45b7e93ef9e2765f112fdf2b7c9821016cdfb380b9445374aed1",
"https://deno.land/std@0.213.0/assert/assert_equals.ts": "4497c56fe7d2993b0d447926702802fc0becb44e319079e8eca39b482ee01b4e",
"https://deno.land/std@0.213.0/assert/assert_exists.ts": "24a7bf965e634f909242cd09fbaf38bde6b791128ece08e33ab08586a7cc55c9",
"https://deno.land/std@0.213.0/assert/assert_false.ts": "6f382568e5128c0f855e5f7dbda8624c1ed9af4fcc33ef4a9afeeedcdce99769",
"https://deno.land/std@0.213.0/assert/assert_greater.ts": "4945cf5729f1a38874d7e589e0fe5cc5cd5abe5573ca2ddca9d3791aa891856c",
"https://deno.land/std@0.213.0/assert/assert_greater_or_equal.ts": "573ed8823283b8d94b7443eb69a849a3c369a8eb9666b2d1db50c33763a5d219",
"https://deno.land/std@0.213.0/assert/assert_instance_of.ts": "72dc1faff1e248692d873c89382fa1579dd7b53b56d52f37f9874a75b11ba444",
"https://deno.land/std@0.213.0/assert/assert_is_error.ts": "6596f2b5ba89ba2fe9b074f75e9318cda97a2381e59d476812e30077fbdb6ed2",
"https://deno.land/std@0.213.0/assert/assert_less.ts": "2b4b3fe7910f65f7be52212f19c3977ecb8ba5b2d6d0a296c83cde42920bb005",
"https://deno.land/std@0.213.0/assert/assert_less_or_equal.ts": "b93d212fe669fbde959e35b3437ac9a4468f2e6b77377e7b6ea2cfdd825d38a0",
"https://deno.land/std@0.213.0/assert/assert_match.ts": "ec2d9680ed3e7b9746ec57ec923a17eef6d476202f339ad91d22277d7f1d16e1",
"https://deno.land/std@0.213.0/assert/assert_not_equals.ts": "f3edda73043bc2c9fae6cbfaa957d5c69bbe76f5291a5b0466ed132c8789df4c",
"https://deno.land/std@0.213.0/assert/assert_not_instance_of.ts": "8f720d92d83775c40b2542a8d76c60c2d4aeddaf8713c8d11df8984af2604931",
"https://deno.land/std@0.213.0/assert/assert_not_match.ts": "b4b7c77f146963e2b673c1ce4846473703409eb93f5ab0eb60f6e6f8aeffe39f",
"https://deno.land/std@0.213.0/assert/assert_not_strict_equals.ts": "da0b8ab60a45d5a9371088378e5313f624799470c3b54c76e8b8abeec40a77be",
"https://deno.land/std@0.213.0/assert/assert_object_match.ts": "e85e5eef62a56ce364c3afdd27978ccab979288a3e772e6855c270a7b118fa49",
"https://deno.land/std@0.213.0/assert/assert_rejects.ts": "e9e0c8d9c3e164c7ac962c37b3be50577c5a2010db107ed272c4c1afb1269f54",
"https://deno.land/std@0.213.0/assert/assert_strict_equals.ts": "0425a98f70badccb151644c902384c12771a93e65f8ff610244b8147b03a2366",
"https://deno.land/std@0.213.0/assert/assert_string_includes.ts": "dfb072a890167146f8e5bdd6fde887ce4657098e9f71f12716ef37f35fb6f4a7",
"https://deno.land/std@0.213.0/assert/assert_throws.ts": "edddd86b39606c342164b49ad88dd39a26e72a26655e07545d172f164b617fa7",
"https://deno.land/std@0.213.0/assert/assertion_error.ts": "9f689a101ee586c4ce92f52fa7ddd362e86434ffdf1f848e45987dc7689976b8",
"https://deno.land/std@0.213.0/assert/equal.ts": "fae5e8a52a11d3ac694bbe1a53e13a7969e3f60791262312e91a3e741ae519e2",
"https://deno.land/std@0.213.0/assert/fail.ts": "f310e51992bac8e54f5fd8e44d098638434b2edb802383690e0d7a9be1979f1c",
"https://deno.land/std@0.213.0/assert/mod.ts": "325df8c0683ad83a873b9691aa66b812d6275fc9fec0b2d180ac68a2c5efed3b",
"https://deno.land/std@0.213.0/assert/unimplemented.ts": "47ca67d1c6dc53abd0bd729b71a31e0825fc452dbcd4fde4ca06789d5644e7fd",
"https://deno.land/std@0.213.0/assert/unreachable.ts": "38cfecb95d8b06906022d2f9474794fca4161a994f83354fd079cac9032b5145",
"https://deno.land/std@0.213.0/fmt/colors.ts": "aeaee795471b56fc62a3cb2e174ed33e91551b535f44677f6320336aabb54fbb"
}
}
41 changes: 41 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,47 @@ Deno.test('client/JSONRPCClientParseError', async () => {
).catch((e) => e),
JSONRPCClientParseError,
)

client = new JSONRPCClient(() =>
JSON.stringify(
{
jsonrpc: '2.0',
id: null,
error: {
code: 0,
message: 'ERR_MSG',
},
},
)
)

assertEquals(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
).catch((e) => e.message),
'ERR_MSG',
)

client = new JSONRPCClient(() =>
JSON.stringify(
{
jsonrpc: '2.0',
id: null,
result: 'wrong server',
},
)
)

assertInstanceOf(
await client.batch(
client.createRequest('foo1'),
client.createRequest('foo2'),
client.createRequest('foo3'),
).catch((e) => e),
JSONRPCClientParseError,
)
})

Deno.test({
Expand Down

0 comments on commit 263bc2c

Please sign in to comment.