@printobserver/sdk 0.8.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +1 -1
- package/dist/client.js +84 -18
- package/dist/contract.d.ts +492 -4
- package/dist/contract.js +6224 -59
- package/dist/shapes.d.ts +21 -0
- package/dist/shapes.js +171 -0
- package/dist/surface.d.ts +10 -9
- package/dist/surface.js +7 -6
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -45,5 +45,5 @@ export declare class Client extends GeneratedClient {
|
|
|
45
45
|
/** What authenticates to it, where anything does. */
|
|
46
46
|
readonly credential: string | undefined;
|
|
47
47
|
constructor(options: ClientOptions);
|
|
48
|
-
protected call<T>(method: string, target: string, asked: Array<[string, string]>, sending: Record<string, unknown> | undefined): Promise<T>;
|
|
48
|
+
protected call<T>(method: string, target: string, asked: Array<[string, string]>, sending: Record<string, unknown> | undefined, answer: string): Promise<T>;
|
|
49
49
|
}
|
package/dist/client.js
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
* transport stack of its own.
|
|
14
14
|
*/
|
|
15
15
|
import { GeneratedClient } from "./contract.js";
|
|
16
|
+
import { conforms } from "./shapes.js";
|
|
16
17
|
import { PrinterRefused, Refused, Rejected, Unreachable, Unreadable } from "./surface.js";
|
|
17
18
|
/** The status a rejected action is answered under. */
|
|
18
19
|
const REJECTED_STATUS = 409;
|
|
@@ -38,7 +39,7 @@ export class Client extends GeneratedClient {
|
|
|
38
39
|
this.origin = origin(options.server);
|
|
39
40
|
this.credential = options.credential;
|
|
40
41
|
}
|
|
41
|
-
async call(method, target, asked, sending) {
|
|
42
|
+
async call(method, target, asked, sending, answer) {
|
|
42
43
|
const headers = { Accept: MEDIA_TYPE };
|
|
43
44
|
if (sending !== undefined) {
|
|
44
45
|
headers["Content-Type"] = MEDIA_TYPE;
|
|
@@ -59,7 +60,12 @@ export class Client extends GeneratedClient {
|
|
|
59
60
|
}
|
|
60
61
|
const said = await answered.text();
|
|
61
62
|
if (answered.status === SUCCESS_STATUS) {
|
|
62
|
-
|
|
63
|
+
const parsed = document(answered.status, said);
|
|
64
|
+
if (!conforms(parsed, answer)) {
|
|
65
|
+
throw new Unreadable(answered.status, `the supervisor answered no ${answer}`);
|
|
66
|
+
}
|
|
67
|
+
// Held above to the type the contracts declare for this answer.
|
|
68
|
+
return parsed;
|
|
63
69
|
}
|
|
64
70
|
if (answered.status === REJECTED_STATUS) {
|
|
65
71
|
throw rejection(answered.status, said);
|
|
@@ -112,26 +118,86 @@ function refusalText(said) {
|
|
|
112
118
|
}
|
|
113
119
|
return "it said nothing this client can read";
|
|
114
120
|
}
|
|
115
|
-
/**
|
|
121
|
+
/** Whether one parsed value is a JSON object, whose members can then be read. */
|
|
122
|
+
function isObject(value) {
|
|
123
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The answer type a refused document declares itself as by its members: an
|
|
127
|
+
* action's answer carries its record, a recovery's names its action beside its
|
|
128
|
+
* decision, and a spool command's carries its decision at the top.
|
|
129
|
+
*/
|
|
130
|
+
function answerType(document) {
|
|
131
|
+
if (isObject(document) && "record" in document) {
|
|
132
|
+
return "ActionAnswer";
|
|
133
|
+
}
|
|
134
|
+
if (isObject(document) && "action" in document) {
|
|
135
|
+
return "RecoveryAnswer";
|
|
136
|
+
}
|
|
137
|
+
return "SpoolCommandAnswer";
|
|
138
|
+
}
|
|
139
|
+
/** A refusal's value asked for and range allowed, when it ruled on a value. */
|
|
140
|
+
function boundsOf(refused) {
|
|
141
|
+
const bounds = isObject(refused) ? refused.out_of_bounds : undefined;
|
|
142
|
+
if (!isObject(bounds) || typeof bounds.requested !== "number" || !isObject(bounds.allowed)) {
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
const { min, max } = bounds.allowed;
|
|
146
|
+
if (typeof min !== "number" || typeof max !== "number") {
|
|
147
|
+
return {};
|
|
148
|
+
}
|
|
149
|
+
return { requested: bounds.requested, allowed: { min, max } };
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The policy's own refusal, as the answer to it carries it.
|
|
153
|
+
*
|
|
154
|
+
* The whole answer is held to the type the contracts declare for it — every
|
|
155
|
+
* member, every closed set of names and arms, every nested shape — before any
|
|
156
|
+
* of it is handed on as that type.
|
|
157
|
+
*/
|
|
116
158
|
function rejection(status, said) {
|
|
117
159
|
const answer = document(status, said);
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if (typeof decision !== "object" || !("rejected" in decision)) {
|
|
122
|
-
return new Unreadable(status, "the supervisor refused this action and answered a record whose decision is not a refusal");
|
|
160
|
+
const declared = answerType(answer);
|
|
161
|
+
if (!isObject(answer) || !conforms(answer, declared)) {
|
|
162
|
+
return new Unreadable(status, `the supervisor refused this and answered no ${declared}`);
|
|
123
163
|
}
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
requested = reason.out_of_bounds.requested;
|
|
129
|
-
allowed = reason.out_of_bounds.allowed;
|
|
164
|
+
const decision = isObject(answer.record) ? answer.record.decision : answer.decision;
|
|
165
|
+
const refused = isObject(decision) ? decision.rejected : undefined;
|
|
166
|
+
if (refused === undefined) {
|
|
167
|
+
return new Unreadable(status, "the supervisor refused this action and answered a record whose decision is not a refusal");
|
|
130
168
|
}
|
|
131
|
-
|
|
169
|
+
const bounds = boundsOf(refused);
|
|
170
|
+
return new Rejected(
|
|
171
|
+
// Held above to the refusal vocabulary its answer type declares.
|
|
172
|
+
refused, bounds.requested, bounds.allowed,
|
|
173
|
+
// Held above to the answer type it declares itself as.
|
|
174
|
+
answer);
|
|
132
175
|
}
|
|
133
|
-
/**
|
|
176
|
+
/**
|
|
177
|
+
* The machine's own refusal of an action the policy accepted.
|
|
178
|
+
*
|
|
179
|
+
* A recovery the machine refused carries no action record, and arrives as the
|
|
180
|
+
* supervisor's own words for it: a recovery answer whose decision was carried
|
|
181
|
+
* out and failed at the printer. Either answer is held to the type the
|
|
182
|
+
* contracts declare for it before any of it is handed on.
|
|
183
|
+
*/
|
|
134
184
|
function machineRefusal(status, said) {
|
|
135
|
-
const
|
|
136
|
-
|
|
185
|
+
const answered = document(status, said);
|
|
186
|
+
const declared = answerType(answered);
|
|
187
|
+
if (declared === "SpoolCommandAnswer" || !isObject(answered) || !conforms(answered, declared)) {
|
|
188
|
+
return new Unreadable(status, "the supervisor answered a refusal carrying neither a record nor words");
|
|
189
|
+
}
|
|
190
|
+
if (declared === "RecoveryAnswer") {
|
|
191
|
+
const { decision, detail } = answered;
|
|
192
|
+
const executed = isObject(decision) ? decision.executed : undefined;
|
|
193
|
+
if (!isObject(executed) || !isObject(executed.failed) || typeof detail !== "string") {
|
|
194
|
+
return new Unreadable(status, "the supervisor answered a machine refusal of a recovery that did not fail");
|
|
195
|
+
}
|
|
196
|
+
return new Refused(status, detail);
|
|
197
|
+
}
|
|
198
|
+
return new PrinterRefused(typeof answered.printer_refusal === "string"
|
|
199
|
+
? answered.printer_refusal
|
|
200
|
+
: "it said nothing this client can read",
|
|
201
|
+
// Held above to the answer type it declares itself as.
|
|
202
|
+
answered);
|
|
137
203
|
}
|