deno.land / x / replicache@v10.0.0-beta.0 / puller.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {assertArray, assertNumber, assertObject, assertString} from './asserts';import {httpRequest} from './http-request';import {assertJSONValue, JSONValue, ReadonlyJSONValue} from './json';import type {HTTPRequestInfo} from './http-request-info';
export type PullerResult = { response?: PullResponse; httpRequestInfo: HTTPRequestInfo;};
/** * Puller is the function type used to do the fetch part of a pull. The request * is a POST request where the body is JSON with the type [[PullRequest]]. */export type Puller = (request: Request) => Promise<PullerResult>;
/** * The shape of a pull response under normal circumstances. */export type PullResponseOK = { cookie?: ReadonlyJSONValue; lastMutationID: number; patch: PatchOperation[];};
/** * In certain scenarios the server can signal that it does not know about the * client. For example, the server might have deleted the client. */export type ClientStateNotFoundResponse = { error: 'ClientStateNotFound';};
/** * PullResponse defines the shape and type of the response of a pull. This is * the JSON you should return from your pull server endpoint. */export type PullResponse = PullResponseOK | ClientStateNotFoundResponse;
export function isClientStateNotFoundResponse( result: unknown,): result is ClientStateNotFoundResponse { return ( typeof result === 'object' && result !== null && (result as Partial<ClientStateNotFoundResponse>).error === 'ClientStateNotFound' );}
export function assertPullResponse(v: unknown): asserts v is PullResponse { if (typeof v !== 'object' || v === null) { throw new Error('PullResponse must be an object'); } if (isClientStateNotFoundResponse(v)) { return; } const v2 = v as Partial<PullResponseOK>; if (v2.cookie !== undefined) { assertJSONValue(v2.cookie); } assertNumber(v2.lastMutationID); assertPatchOperations(v2.patch);}
/** * This type describes the patch field in a [[PullResponse]] and it is used * to describe how to update the Replicache key-value store. */export type PatchOperation = | { op: 'put'; key: string; value: JSONValue; } | {op: 'del'; key: string} | {op: 'clear'};
export const defaultPuller: Puller = async request => { const {httpRequestInfo, response} = await httpRequest(request); if (httpRequestInfo.httpStatusCode !== 200) { return { httpRequestInfo, }; } return { response: await response.json(), httpRequestInfo, };};
export function assertPatchOperations( p: unknown,): asserts p is PatchOperation[] { assertArray(p); for (const item of p) { assertPatchOperation(item); }}
function assertPatchOperation(p: unknown): asserts p is PatchOperation { assertObject(p); switch (p.op) { case 'put': assertString(p.key); assertJSONValue(p.value); break; case 'del': assertString(p.key); break; case 'clear': break; default: throw new Error( `unknown patch op \`${p.op}\`, expected one of \`put\`, \`del\`, \`clear\``, ); }}
/** * This error is thrown when the puller fails for any reason. */export class PullError extends Error { name = 'PullError'; // causedBy is used instead of cause, because while cause has been proposed as a // JavaScript language standard for this purpose (see // https://github.com/tc39/proposal-error-cause) current browser behavior is // inconsistent. causedBy?: Error; constructor(causedBy?: Error) { super('Failed to pull'); this.causedBy = causedBy; }}
replicache

Version Info

Tagged at
2 years ago