Skip to content

Commit dd5fde5

Browse files
committed
feat: update share endpoint to support more things
1 parent 18f9959 commit dd5fde5

File tree

5 files changed

+206
-59
lines changed

5 files changed

+206
-59
lines changed

doc/api/type-tagged.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ anywhere.
3030
## Specification
3131

3232
- The `"$"` key indicates a type (or class) of object
33-
- Any key beginning with `$` is a **meta-key**
33+
- Any other key beginning with `$` is a **meta-key**
3434
- Other keys are not allowed to contain `$`
3535
- `"$version"` must follow [semver](https://semver.org/)
36+
- Keys with multiple `"$"` symbols are reserved for future use
3637

3738
## Alternative Representations
3839

packages/backend/src/CoreModule.js

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ const install = async ({ services, app, useapi }) => {
5757
const ArrayUtil = require('./libraries/ArrayUtil');
5858
services.registerService('util-array', ArrayUtil);
5959

60+
const LibTypeTagged = require('./libraries/LibTypeTagged');
61+
services.registerService('lib-type-tagged', LibTypeTagged);
62+
6063
// === SERVICES ===
6164

6265
// /!\ IMPORTANT /!\

packages/backend/src/api/APIError.js

+23
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ module.exports = class APIError {
3333
status: 500,
3434
message: () => `An unknown error occurred`,
3535
},
36+
'format_error': {
37+
status: 400,
38+
message: ({ message }) => `format error: ${message}`,
39+
},
40+
'temp_error': {
41+
status: 400,
42+
message: ({ message }) => `error: ${message}`,
43+
},
44+
45+
// Things
46+
'disallowed_thing': {
47+
status: 400,
48+
message: ({ thing_type, accepted }) =>
49+
`Request contained a ${quot(thing_type)} in a ` +
50+
`place where ${quot(thing_type)} isn't accepted` +
51+
(
52+
accepted
53+
? '; ' +
54+
'accepted types are: ' +
55+
accepted.map(v => quot(v)).join(', ')
56+
: ''
57+
) + '.'
58+
},
3659

3760
// Unorganized
3861
'item_with_same_name_exists': {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const Library = require("../definitions/Library");
2+
const { whatis } = require("../util/langutil");
3+
4+
class LibTypeTagged extends Library {
5+
process (o) {
6+
const could_be = whatis(o) === 'object' || Array.isArray(o);
7+
if ( ! could_be ) return {
8+
$: 'error',
9+
code: 'invalid-type',
10+
message: 'should be object or array',
11+
};
12+
13+
const intermediate = this.get_intermediate_(o);
14+
15+
if ( ! intermediate.type ) return {
16+
$: 'error',
17+
code: 'missing-type-param',
18+
message: 'type parameter is missing',
19+
};
20+
21+
return this.intermediate_to_standard_(intermediate);
22+
}
23+
24+
intermediate_to_standard_ (intermediate) {
25+
const out = {};
26+
out.$ = intermediate.type;
27+
for ( const k in intermediate.meta ) {
28+
out['$' + k] = intermediate.meta[k];
29+
}
30+
for ( const k in intermediate.body ) {
31+
out[k] = intermediate.body[k];
32+
}
33+
return out;
34+
}
35+
36+
get_intermediate_ (o) {
37+
if ( Array.isArray(o) ) {
38+
return this.process_array_(o);
39+
}
40+
41+
if ( o['$'] === '$meta-body' ) {
42+
return this.process_structured_(o);
43+
}
44+
45+
return this.process_standard_(o);
46+
}
47+
48+
process_array_ (a) {
49+
if ( a.length <= 1 || a.length > 3 ) return {
50+
$: 'error',
51+
code: 'invalid-array-length',
52+
message: 'tag-typed arrays should have 1-3 elements',
53+
};
54+
55+
const [type, body = {}, meta = {}] = a;
56+
57+
return { $: '$', type, body, meta };
58+
}
59+
60+
process_structured_ (o) {
61+
if ( ! o.hasOwnProperty('type') ) return {
62+
$: 'error',
63+
code: 'missing-type-property',
64+
message: 'missing "type" property'
65+
};
66+
67+
return { $: '$', ...o };
68+
}
69+
70+
process_standard_ (o) {
71+
const type = o.$;
72+
const meta = {};
73+
const body = {};
74+
75+
for ( const k in o ) {
76+
if ( k === '$' ) continue;
77+
if ( k.startsWith('$') ) {
78+
meta[k.slice(1)] = o[k];
79+
} else {
80+
body[k] = o[k];
81+
}
82+
}
83+
84+
return { $: '$', type, meta, body };
85+
}
86+
}
87+
88+
module.exports = LibTypeTagged;

0 commit comments

Comments
 (0)