Skip to content

Commit ef9487a

Browse files
committed
better comments
1 parent 7dfd00c commit ef9487a

File tree

8 files changed

+110
-47
lines changed

8 files changed

+110
-47
lines changed

build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Deno.writeTextFileSync('./package.json', JSON.stringify(packageJson, null, 4))
4343
console.log('New package.json created.')
4444
console.log(packageJson)
4545

46-
const builderName = 'unbuild@3.5.0'
46+
const builderName = 'unbuild@3.6.1'
4747
console.log(`Building package using ${builderName}...`)
4848
new Deno.Command('npx', {
4949
args: ['-y', builderName],

deno.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yieldray/json-rpc-ts",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"exports": "./mod.ts",
55
"publish": {
66
"include": [
@@ -20,7 +20,7 @@
2020
"useUnknownInCatchVariables": true,
2121
"noImplicitOverride": true
2222
},
23-
"imports": { "@std/assert": "jsr:@std/assert@^1.0.13" },
23+
"imports": { "@std/assert": "jsr:@std/assert@^1.0.14" },
2424
"tasks": {
2525
"lint": "deno lint",
2626
"fmt": "deno fmt",

deno.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ import {
1313
} from './id.ts'
1414

1515
/**
16-
* Provide a external `requestForResponse` function to the constructor,
17-
* it should accept a `string` (json encoded from one or more json rpc request)
18-
* and your customized function should send this string to a json rpc server
19-
* for any response represented as `string`
16+
* Provide an external `requestForResponse` function to the constructor.
17+
* This function should accept a `string` (JSON-encoded from one or more JSON-RPC requests),
18+
* and your custom implementation should send this string to a JSON-RPC server,
19+
* returning any response as a `string`.
2020
*
21-
* The constructor optionally accept a customized id generator, otherwise it use a
22-
* self added number
21+
* The constructor optionally accepts a custom ID generator; otherwise, it uses
22+
* an internally generated number.
2323
*
24-
* To customize the request or response, you can extend `JSONRPCClient`.
24+
* To customize requests or responses, extend the `JSONRPCClient` class.
2525
*
26-
* To customize request, overwrite `createRequest` and `createNotification` methods.
26+
* To customize requests, override the `createRequest` and `createNotification` methods.
2727
*
28-
* To customize response, overwrite `request` `notify` and `batch` methods.
28+
* To customize responses, override the `request`, `notify`, and `batch` methods.
2929
*/
3030
export class JSONRPCClient<
3131
Methods extends JSONRPCMethods = JSONRPCMethods,
3232
> {
3333
/**
34-
* MUST be an infinite iterator
34+
* MUST be an infinite iterator.
3535
*/
3636
protected idGenerator: IDGenerator
3737
/**
38-
* The external function to send the json string to any rpc server,
39-
* and fetch for response as string
38+
* The external function that sends the JSON string to any RPC server
39+
* and fetches the response as a string.
4040
*/
4141
protected requestForResponse: (input: string) => string | Promise<string>
4242

4343
/**
44-
* @param requestForResponse An external function to send the json string to any rpc server,
45-
* and fetch for response as string
44+
* @param requestForResponse An external function that sends the JSON string to any RPC server
45+
* and fetches the response as a string.
4646
*/
4747
public constructor(
4848
requestForResponse: (input: string) => string | Promise<string>,
@@ -68,6 +68,9 @@ export class JSONRPCClient<
6868
return request
6969
}
7070

71+
/**
72+
* Creates a JSON-RPC notification object.
73+
*/
7174
public createNotification<T extends keyof Methods>(
7275
method: T extends string ? T : never,
7376
params: Parameters<Methods[T]>[0],
@@ -80,7 +83,8 @@ export class JSONRPCClient<
8083
}
8184

8285
/**
83-
* Send `JSONRPCRequest` to server, returns `JSONRPCValue` or throw `JSONRPCErrorInterface` (or `JSONRPCClientParseError`)
86+
* Sends a `JSONRPCRequest` to the server and returns a `JSONRPCValue`,
87+
* or throws a `JSONRPCErrorInterface` (or `JSONRPCClientParseError`).
8488
*/
8589
public async request<T extends keyof Methods>(
8690
method: T extends string ? T : never,
@@ -120,8 +124,8 @@ export class JSONRPCClient<
120124
}
121125

122126
/**
123-
* Send `JSONRPCNotification` to server, no returns,
124-
* only throws if your provided `processor` function throws
127+
* Sends a `JSONRPCNotification` to the server. No return value.
128+
* Throws only if your provided `processor` function throws.
125129
*/
126130
public async notify<T extends keyof Methods>(
127131
method: T extends string ? T : never,
@@ -132,14 +136,13 @@ export class JSONRPCClient<
132136
}
133137

134138
/**
135-
* You should use the `createRequest()` or `createNotification()` method to
136-
* create the requests array. Response order is always matched by id.
139+
* Use the `createRequest()` or `createNotification()` method to create the requests array.
140+
* Response order is always matched by ID.
137141
*
138-
* Throws `JSONRPCClientParseError` if server response cannot be parsed,
139-
* note that it does not throws for any `JSONRPCErrorResponse`, in this
140-
* case it will be a single object: `{ status: 'rejected', reason: {...} }`
142+
* Throws `JSONRPCClientParseError` if the server response cannot be parsed.
143+
* Note: It does not throw for any `JSONRPCErrorResponse`; in this case, it will be a single object: `{ status: 'rejected', reason: {...} }`
141144
*
142-
* Usually it returns be like (same as the `Promise.allSettled()` method):
145+
* The return value is similar to the `Promise.allSettled()` method:
143146
* ```js
144147
* [
145148
* { status: 'fulfilled', value: '...' },
@@ -152,8 +155,9 @@ export class JSONRPCClient<
152155
* },
153156
* ]
154157
* ```
155-
* @throws `JSONRPCError` - when server return single JSONRPCErrorResponse
156-
* @throws `JSONRPCClientParseError` - when server response cannot be parsed
158+
* @throws `JSONRPCError` - when the server returns a single JSONRPCErrorResponse
159+
* @throws `JSONRPCClientParseError` - when the server response cannot be parsed
160+
* @see https://www.jsonrpc.org/specification#batch
157161
*/
158162
public async batch(
159163
...requests: Array<JSONRPCRequest | JSONRPCNotification>
@@ -244,13 +248,16 @@ export class JSONRPCClient<
244248
}
245249
}
246250

251+
/**
252+
* A union type representing any JSON-RPC request, either a notification or a request.
253+
*/
247254
type JSONRPCAnyRequest =
248255
| JSONRPCNotification
249256
| JSONRPCRequest
250257
| Array<JSONRPCNotification | JSONRPCRequest>
251258

252259
/**
253-
* The client cannot parse the server response
260+
* Indicates that the client cannot parse the server response.
254261
*/
255262
export class JSONRPCClientParseError extends Error {
256263
override name = 'JSONRPCClientParseError'
@@ -262,7 +269,8 @@ export class JSONRPCClientParseError extends Error {
262269
}
263270

264271
/**
265-
* Just wrap the JSON.parse function, with potential `JSONRPCClientParseError`
272+
* Wraps JSON.parse to provide better error handling.
273+
* Throws JSONRPCClientParseError if the response is not valid JSON.
266274
*/
267275
function parseJSON(
268276
text: string,

src/dto/errors.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { JSONRPCValue } from '../types.ts'
22

3+
/**
4+
* @see https://www.jsonrpc.org/specification#error_object
5+
* When a rpc call encounters an error, the Response Object MUST contain the error member with a value that is a Object with the following members:
6+
*/
37
export interface JSONRPCErrorInterface {
48
/**
59
* A Number that indicates the error type that occurred.
@@ -19,6 +23,23 @@ export interface JSONRPCErrorInterface {
1923
data?: JSONRPCValue
2024
}
2125

26+
/**
27+
* To define your own JSON-RPC error, you can extend this class with your custom error code (in the range -32000 to -32099).
28+
* @example
29+
* ```ts
30+
* // Reserved for implementation-defined server-errors.
31+
* class MyCustomError extends JSONRPCError {
32+
* constructor(data?: JSONRPCValue) {
33+
* super({
34+
* code: -32001, // -32000 to -32099
35+
* message: 'Server error',
36+
* data: data,
37+
* })
38+
* }
39+
* }
40+
* ```
41+
* @see https://www.jsonrpc.org/specification#error_object
42+
*/
2243
export class JSONRPCError extends Error implements JSONRPCErrorInterface {
2344
/**
2445
* The error codes from and including -32768 to -32000 are reserved for pre-defined errors. Any code within this range, but not defined explicitly below is reserved for future use. The error codes are nearly the same as those suggested for XML-RPC at the following url: http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php
@@ -36,9 +57,9 @@ export class JSONRPCError extends Error implements JSONRPCErrorInterface {
3657
}
3758

3859
/**
39-
* This function ONLY check that if `x` is `JSONRPCErrorInterface`
60+
* This function ONLY checks whether `x` is a `JSONRPCErrorInterface`.
4061
*
41-
* Because it'a shared method between client and server, but `JSONRPCError` is server side only
62+
* `JSONRPCErrorInterface` is shared between client and server, but `JSONRPCError` is server-side only.
4263
*/
4364
export function isJSONRPCError(x: unknown): x is JSONRPCErrorInterface {
4465
if (!x || typeof x !== 'object') {
@@ -54,6 +75,11 @@ export function isJSONRPCError(x: unknown): x is JSONRPCErrorInterface {
5475
return false
5576
}
5677

78+
/**
79+
* Invalid JSON was received by the server.
80+
* An error occurred on the server while parsing the JSON text.
81+
* @see https://www.jsonrpc.org/specification#error_object
82+
*/
5783
export class JSONRPCParseError extends JSONRPCError {
5884
constructor(data?: JSONRPCValue) {
5985
super({
@@ -64,6 +90,10 @@ export class JSONRPCParseError extends JSONRPCError {
6490
}
6591
}
6692

93+
/**
94+
* The JSON sent is not a valid Request object.
95+
* @see https://www.jsonrpc.org/specification#error_object
96+
*/
6797
export class JSONRPCInvalidRequestError extends JSONRPCError {
6898
constructor(data?: JSONRPCValue) {
6999
super({
@@ -74,6 +104,10 @@ export class JSONRPCInvalidRequestError extends JSONRPCError {
74104
}
75105
}
76106

107+
/**
108+
* The method does not exist / is not available.
109+
* @see https://www.jsonrpc.org/specification#error_object
110+
*/
77111
export class JSONRPCMethodNotFoundError extends JSONRPCError {
78112
constructor(data?: JSONRPCValue) {
79113
super({
@@ -84,6 +118,10 @@ export class JSONRPCMethodNotFoundError extends JSONRPCError {
84118
}
85119
}
86120

121+
/**
122+
* Invalid method parameter(s).
123+
* @see https://www.jsonrpc.org/specification#error_object
124+
*/
87125
export class JSONRPCInvalidParamsError extends JSONRPCError {
88126
constructor(data?: JSONRPCValue) {
89127
super({
@@ -94,6 +132,10 @@ export class JSONRPCInvalidParamsError extends JSONRPCError {
94132
}
95133
}
96134

135+
/**
136+
* Internal JSON-RPC error.
137+
* @see https://www.jsonrpc.org/specification#error_object
138+
*/
97139
export class JSONRPCInternalError extends JSONRPCError {
98140
constructor(data?: JSONRPCValue) {
99141
super({

src/dto/request.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { JSONRPCValue, WithOptionalJSONRPCVersion } from '../types.ts'
22
import { isJSONRPCID, type JSONRPCID } from '../id.ts'
33

44
/**
5-
* You should create this object via method from `JSONRPCClient`, it generate id for you.
5+
* You should create this object using a method from `JSONRPCClient`, which generates the ID for you.
66
*
7-
* It's less likely that you need to create this object manually.
7+
* It is unlikely that you will need to create this object manually.
88
*/
99
export class JSONRPCNotification {
1010
public jsonrpc = '2.0' as const
@@ -34,9 +34,10 @@ export class JSONRPCNotification {
3434
}
3535

3636
/**
37-
* You should create this object via method from `JSONRPCClient`, it generate id for you.
37+
* You should create this object using a method from `JSONRPCClient`, which generates the ID for you.
3838
*
39-
* It's less likely that you need to create this object manually.
39+
* It is unlikely that you will need to create this object manually.
40+
* @see https://www.jsonrpc.org/specification#request_object
4041
*/
4142
export class JSONRPCRequest extends JSONRPCNotification {
4243
/**
@@ -60,9 +61,9 @@ export class JSONRPCRequest extends JSONRPCNotification {
6061
}
6162

6263
/**
63-
* WARN: this check if `x` is `JSONRPCNotification` or `JSONRPCRequest`
64+
* WARNING: This function checks if `x` is a `JSONRPCNotification` or a `JSONRPCRequest`.
6465
*
65-
* To check if `x` is `JSONRPCRequest`, ONLY need to check `'id' in x`
66+
* To determine if `x` is a `JSONRPCRequest`, you ONLY need to check if `'id' in x`.
6667
*/
6768
export function isJSONRPCRequest(
6869
x: unknown,

src/dto/response.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { JSONRPCValue, WithOptionalJSONRPCVersion } from '../types.ts'
22
import { isJSONRPCID, type JSONRPCID } from '../id.ts'
33
import { isJSONRPCError, type JSONRPCErrorInterface } from './errors.ts'
44

5+
/**
6+
* @see https://www.jsonrpc.org/specification#response_object
7+
*/
58
export class JSONRPCSuccessResponse {
69
public jsonrpc = '2.0' as const
710
public id: JSONRPCID
@@ -26,6 +29,9 @@ export class JSONRPCSuccessResponse {
2629
}
2730
}
2831

32+
/**
33+
* @see https://www.jsonrpc.org/specification#response_object
34+
*/
2935
export class JSONRPCErrorResponse {
3036
public jsonrpc = '2.0' as const
3137
/**
@@ -54,8 +60,14 @@ export class JSONRPCErrorResponse {
5460
}
5561
}
5662

63+
/**
64+
* @see https://www.jsonrpc.org/specification#response_object
65+
*/
5766
export type JSONRPCResponse = JSONRPCSuccessResponse | JSONRPCErrorResponse
5867

68+
/**
69+
* Checks whether the given value is a valid JSON-RPC 2.0 Response object.
70+
*/
5971
export function isJSONRPCResponse(x: unknown): x is JSONRPCResponse {
6072
if (!x || typeof x !== 'object') {
6173
return false

src/id.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export function* selfAddIdGenerator(): Generator<number, void, unknown> {
1414
}
1515

1616
/**
17-
* This can be but not mean to javascript Generator
18-
* Just means a source of ID
17+
* This does not necessarily refer to a JavaScript Generator;
18+
* it simply represents any source of IDs.
1919
*/
2020
export type IDGenerator = Iterator<JSONRPCID> | (() => JSONRPCID)
2121

0 commit comments

Comments
 (0)