@modernpoacher/halacious 7.0.1 → 7.0.3

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.
@@ -1,2 +0,0 @@
1
- # A boss
2
- ## sub title
@@ -1,2 +0,0 @@
1
- # An employee
2
- ## sub title
@@ -1,384 +0,0 @@
1
- import * as chai from 'chai'
2
- import hapi from '@hapi/hapi'
3
- import halacious from '#halacious'
4
- import RepresentationFactory from '#halacious/representation'
5
-
6
- const should = chai.should()
7
-
8
- const PLUGIN = 'halacious' // '@modernpoacher/halacious'
9
-
10
- describe('Representation Factory', () => {
11
- let server
12
- let representationFactory
13
-
14
- beforeEach(async () => {
15
- server = hapi.server({ port: 9191 })
16
-
17
- await server.register(halacious)
18
-
19
- const {
20
- plugins: {
21
- [PLUGIN]: plugin
22
- }
23
- } = server
24
-
25
- representationFactory = new RepresentationFactory(plugin)
26
- })
27
-
28
- afterEach(async () => {
29
- await server.stop()
30
- })
31
-
32
- it('should create a new representation', () => {
33
- should.exist(representationFactory)
34
- const entity = { firstName: 'Bob', lastName: 'Smith' }
35
- const rep = representationFactory.create(entity, '/people')
36
- rep._links.should.have.property('self')
37
- rep._links.self.should.have.property('href', '/people')
38
- rep.should.have.property('entity', entity)
39
- })
40
-
41
- it('should serialize a simple entity into property JSON', () => {
42
- const entity = { firstName: 'Bob', lastName: 'Smith' }
43
- const rep = representationFactory.create(entity, '/people')
44
- const json = JSON.stringify(rep)
45
- json.should.deep.equal(
46
- '{"_links":{"self":{"href":"/people"}},"firstName":"Bob","lastName":"Smith"}'
47
- )
48
- })
49
-
50
- it('should create an array of like-named links', () => {
51
- const entity = {}
52
- const rep = representationFactory.create(entity, '/people')
53
- rep.link('mco:boss', '/people/100')
54
- rep.link('mco:boss', '/people/101')
55
- rep.link('mco:boss', '/people/102')
56
- rep._links['mco:boss'].should.have.length(3)
57
- rep._links['mco:boss'][0].should.have.property('href', '/people/100')
58
- rep._links['mco:boss'][1].should.have.property('href', '/people/101')
59
- rep._links['mco:boss'][2].should.have.property('href', '/people/102')
60
- })
61
-
62
- it('should create a single-element array of links', () => {
63
- const entity = {}
64
- const rep = representationFactory.create(entity, '/people')
65
- rep.link('mco:boss', ['/people/100'])
66
- rep._links['mco:boss'].should.have.length(1)
67
- rep._links['mco:boss'][0].should.have.property('href', '/people/100')
68
- })
69
-
70
- it('should create an array of like-named embeds', () => {
71
- const entity = {}
72
- const rep = representationFactory.create(entity, '/people')
73
- rep.embed('mco:boss', '/people/100', {})
74
- rep.embed('mco:boss', '/people/101', {})
75
- rep.embed('mco:boss', '/people/102', {})
76
- rep._embedded['mco:boss'].should.have.length(3)
77
- rep._embedded['mco:boss'][0]._links.self.should.have.property(
78
- 'href',
79
- '/people/100'
80
- )
81
- rep._embedded['mco:boss'][1]._links.self.should.have.property(
82
- 'href',
83
- '/people/101'
84
- )
85
- rep._embedded['mco:boss'][2]._links.self.should.have.property(
86
- 'href',
87
- '/people/102'
88
- )
89
- })
90
-
91
- it('should ignore properties', () => {
92
- const obj = { id: 100, first: 'John', last: 'Smith' }
93
- const rep = representationFactory.create(obj, '/people')
94
- rep.ignore('id', 'first')
95
- const json = JSON.stringify(rep)
96
- json.should.deep.equal(
97
- '{"_links":{"self":{"href":"/people"}},"last":"Smith"}'
98
- )
99
- })
100
-
101
- it('should support extra properties', () => {
102
- const obj = { id: 100, first: 'John', last: 'Smith' }
103
- const rep = representationFactory.create(obj, '/people')
104
- rep.ignore('id')
105
- rep.prop('company', 'ACME')
106
- const json = JSON.stringify(rep)
107
- json.should.deep.equal(
108
- '{"_links":{"self":{"href":"/people"}},"first":"John","last":"Smith","company":"ACME"}'
109
- )
110
- })
111
-
112
- it('should support objects with custom json serialization', () => {
113
- const entity = {
114
- _id: 100,
115
- _hidden: 'hidden',
116
- name: 'John Smith',
117
- company: 'Acme',
118
- toJSON () {
119
- return {
120
- id: this._id,
121
- name: this.name,
122
- company: this.company,
123
- boss: this.boss
124
- }
125
- },
126
- boss: {
127
- _id: 100,
128
- _hidden: 'hidden',
129
- name: 'Boss Man',
130
- company: 'Acme',
131
- toJSON () {
132
- return {
133
- id: this._id,
134
- name: this.name,
135
- company: this.company
136
- }
137
- }
138
- }
139
- }
140
-
141
- const rep = representationFactory.create(entity, '/me')
142
- const json = JSON.stringify(rep)
143
- json.should.deep.equal(
144
- '{"_links":{"self":{"href":"/me"}},"id":100,"name":"John Smith","company":"Acme","boss":{"id":100,"name":"Boss Man","company":"Acme"}}'
145
- )
146
- })
147
-
148
- it('should handle direct call toJSON correctly', () => {
149
- const entity = {
150
- _id: 100,
151
- _hidden: 'hidden',
152
- name: 'John Smith',
153
- company: 'Acme',
154
- toJSON () {
155
- return {
156
- id: this._id,
157
- name: this.name,
158
- company: this.company
159
- }
160
- }
161
- }
162
-
163
- const boss = {
164
- _id: 100,
165
- _hidden: 'hidden',
166
- name: 'Boss Man',
167
- company: 'Acme',
168
- toJSON () {
169
- return {
170
- id: this._id,
171
- name: this.name,
172
- company: this.company
173
- }
174
- }
175
- }
176
-
177
- const rep = representationFactory.create(entity, '/me')
178
-
179
- // Should embed array of objects correctly
180
- rep.embed('mco:boss', './boss', [boss])
181
-
182
- // Should embed single object correctly
183
- rep.embed('mco:boss2', './boss2', boss)
184
-
185
- const json = rep.toJSON()
186
- json.should.deep.equal({
187
- _links: { self: { href: '/me' } },
188
- id: 100,
189
- name: 'John Smith',
190
- company: 'Acme',
191
- _embedded: {
192
- 'mco:boss': [
193
- {
194
- _links: { self: { href: '/me/boss' } },
195
- id: 100,
196
- name: 'Boss Man',
197
- company: 'Acme'
198
- }
199
- ],
200
- 'mco:boss2': {
201
- _links: { self: { href: '/me/boss2' } },
202
- id: 100,
203
- name: 'Boss Man',
204
- company: 'Acme'
205
- }
206
- }
207
- })
208
- })
209
-
210
- it('should link to a registered rel', () => {
211
- const {
212
- plugins: {
213
- [PLUGIN]: plugin
214
- }
215
- } = server
216
-
217
- plugin.namespaces
218
- .add({ name: 'mycompany', prefix: 'mco' })
219
- .rel({ name: 'boss' })
220
-
221
- const entity = { firstName: 'Bob', lastName: 'Smith' }
222
- const rep = representationFactory.create(entity, '/people')
223
- rep.link('mco:boss', '/people/1234')
224
- rep._links.should.have.property('mco:boss')
225
- rep._links['mco:boss'].should.have.property('href', '/people/1234')
226
- })
227
-
228
- it('should not break when linking an empty array', () => {
229
- const rep = representationFactory.create({ firstName: 'Bob' }, '/people')
230
- rep.link('employees', [])
231
- rep._links.should.have.property('employees').that.has.length(0)
232
- })
233
-
234
- it('should resolve relative paths', () => {
235
- const entity = { firstName: 'Bob', lastName: 'Smith' }
236
- const rep = representationFactory.create(entity, '/people')
237
- rep.resolve('./1234').should.equal('/people/1234')
238
- rep.resolve('../1234').should.equal('/1234')
239
- rep.resolve('/companies/100').should.equal('/companies/100')
240
- })
241
-
242
- it('should include a curie link', () => {
243
- const {
244
- plugins: {
245
- [PLUGIN]: plugin
246
- }
247
- } = server
248
-
249
- plugin.namespaces
250
- .add({ name: 'mycompany', prefix: 'mco' })
251
- .rel({ name: 'boss' })
252
-
253
- const rep = representationFactory.create({}, '/people')
254
- rep.link('mco:boss', '/people/1234')
255
- const json = JSON.stringify(rep)
256
- json.should.deep.equal(
257
- '{"_links":{"self":{"href":"/people"},"curies":[{"name":"mco","href":"/rels/mycompany/{rel}","templated":true}],"mco:boss":{"href":"/people/1234"}}}'
258
- )
259
- })
260
-
261
- it('should embed an entity', () => {
262
- const {
263
- plugins: {
264
- [PLUGIN]: plugin
265
- }
266
- } = server
267
-
268
- plugin.namespaces
269
- .add({ name: 'mycompany', prefix: 'mco' })
270
- .rel({ name: 'boss' })
271
-
272
- const rep = representationFactory.create(
273
- { firstName: 'Bob', lastName: 'Smith' },
274
- '/people/me'
275
- )
276
- rep.embed('mco:boss', './boss', {
277
- firstName: 'Boss',
278
- lastName: 'Man'
279
- })
280
-
281
- const json = JSON.stringify(rep)
282
- const obj = JSON.parse(json)
283
- obj.should.deep.equal({
284
- _links: {
285
- self: { href: '/people/me' },
286
- curies: [
287
- { name: 'mco', href: '/rels/mycompany/{rel}', templated: true }
288
- ]
289
- },
290
- firstName: 'Bob',
291
- lastName: 'Smith',
292
- _embedded: {
293
- 'mco:boss': {
294
- _links: { self: { href: '/people/me/boss' } },
295
- firstName: 'Boss',
296
- lastName: 'Man'
297
- }
298
- }
299
- })
300
- })
301
-
302
- it('should embed an empty array', () => {
303
- const {
304
- plugins: {
305
- [PLUGIN]: plugin
306
- }
307
- } = server
308
-
309
- plugin.namespaces
310
- .add({ name: 'mycompany', prefix: 'mco' })
311
- .rel({ name: 'boss' })
312
-
313
- const rep = representationFactory.create(
314
- { firstName: 'Bob', lastName: 'Smith' },
315
- '/people/me'
316
- )
317
- rep.embed('mco:boss', './boss', [])
318
-
319
- const json = JSON.stringify(rep)
320
- const obj = JSON.parse(json)
321
- obj.should.deep.equal({
322
- _links: {
323
- self: { href: '/people/me' },
324
- curies: [
325
- { name: 'mco', href: '/rels/mycompany/{rel}', templated: true }
326
- ]
327
- },
328
- firstName: 'Bob',
329
- lastName: 'Smith',
330
- _embedded: {
331
- 'mco:boss': []
332
- }
333
- })
334
- })
335
-
336
- it('should use top level curie link', () => {
337
- const {
338
- plugins: {
339
- [PLUGIN]: plugin
340
- }
341
- } = server
342
-
343
- plugin.namespaces
344
- .add({ name: 'mycompany', prefix: 'mco' })
345
- .rel({ name: 'boss' })
346
-
347
- plugin.namespaces
348
- .add({ name: 'google', prefix: 'goog' })
349
- .rel({ name: 'profile' })
350
-
351
- const rep = representationFactory.create(
352
- { firstName: 'Bob', lastName: 'Smith' },
353
- '/people/me'
354
- )
355
- const boss = rep.embed('mco:boss', './boss', {
356
- firstName: 'Boss',
357
- lastName: 'Man'
358
- })
359
- boss.link('goog:profile', 'http://users.google.com/BossMan')
360
- const json = JSON.stringify(rep)
361
- const obj = JSON.parse(json)
362
- obj.should.deep.equal({
363
- _links: {
364
- self: { href: '/people/me' },
365
- curies: [
366
- { name: 'mco', href: '/rels/mycompany/{rel}', templated: true },
367
- { name: 'goog', href: '/rels/google/{rel}', templated: true }
368
- ]
369
- },
370
- firstName: 'Bob',
371
- lastName: 'Smith',
372
- _embedded: {
373
- 'mco:boss': {
374
- _links: {
375
- self: { href: '/people/me/boss' },
376
- 'goog:profile': { href: 'http://users.google.com/BossMan' }
377
- },
378
- firstName: 'Boss',
379
- lastName: 'Man'
380
- }
381
- }
382
- })
383
- })
384
- })
package/tsconfig.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "NodeNext",
4
- "target": "ESNext",
5
- "moduleResolution": "NodeNext",
6
- "noEmit": true,
7
- "strict": true,
8
- "isolatedModules": true,
9
- "baseUrl": "."
10
- }
11
- }
@@ -1,11 +0,0 @@
1
- doctype html
2
- body
3
- h1 Namespaces
4
- ul
5
- each namespace in namespaces
6
- li
7
- h2= namespace.name
8
- =namespace.description
9
- ul
10
- each rel in namespace.rels
11
- li #[a(href=path + "/" + namespace.name + "/" + rel.name) #{rel.name}] #{rel.description}
package/views/rel.jade DELETED
@@ -1,7 +0,0 @@
1
- doctype html
2
- head
3
- title=rel.name
4
- body
5
- h1=rel.name
6
- =rel.description
7
- !=relData