@modernpoacher/halacious 7.0.201 → 7.0.203

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modernpoacher/halacious",
3
- "version": "7.0.201",
3
+ "version": "7.0.203",
4
4
  "description": "A HAL processor for Hapi",
5
5
  "keywords": [
6
6
  "HAL",
@@ -57,7 +57,7 @@
57
57
  "@babel/preset-env": "^7.26.9",
58
58
  "@hapi/hapi": "^21.4.0",
59
59
  "@hapi/vision": "^7.0.3",
60
- "@sequencemedia/sinon-chai": "^0.0.133",
60
+ "@sequencemedia/sinon-chai": "^0.0.135",
61
61
  "@types/negotiator": "^0.6.3",
62
62
  "@typescript-eslint/eslint-plugin": "7.18.0",
63
63
  "@typescript-eslint/parser": "7.18.0",
package/src/plugin.mjs CHANGED
@@ -151,7 +151,11 @@ const namespaceSchema = joi.object({
151
151
 
152
152
  if (!hasName(rel)) rel.name = (rel.file && path.basename(rel.file, path.extname(rel.file)))
153
153
 
154
- const { error, value } = relSchema.validate(rel)
154
+ const {
155
+ error,
156
+ value
157
+ } = relSchema.validate(rel)
158
+
155
159
  if (error) throw error
156
160
 
157
161
  rel = value
@@ -159,7 +163,7 @@ const namespaceSchema = joi.object({
159
163
  const relName = getName(rel)
160
164
  const rels = getRels(this)
161
165
 
162
- Reflect.set(rels, relName, rel)
166
+ rels[relName] = rel /* Reflect.set(rels, relName, rel) */
163
167
 
164
168
  rel.namespace = this
165
169
 
@@ -372,8 +376,8 @@ export const plugin = {
372
376
  // shorthand prepare function
373
377
  if (_.isFunction(config)) config = { prepare: config }
374
378
 
375
- if (Reflect.has(config, 'links')) {
376
- const links = Reflect.get(config, 'links')
379
+ if ('links' in config) { /* Reflect.has(config, 'links') */
380
+ const links = config.links /* Reflect.get(config, 'links') */
377
381
 
378
382
  const entity = getRepresentationEntity(representation)
379
383
 
@@ -390,9 +394,9 @@ export const plugin = {
390
394
  })
391
395
  }
392
396
 
393
- if (Reflect.has(config, 'embedded')) {
397
+ if ('embedded' in config) { /* Reflect.has(config, 'embedded') */
394
398
  // configure embedded declarations. each rel entry is also a representation config object
395
- const embedded = Reflect.get(config, 'embedded')
399
+ const embedded = config.embedded /* Reflect.get(config, 'embedded') */
396
400
 
397
401
  const entity = getRepresentationEntity(representation)
398
402
 
@@ -718,6 +722,7 @@ export const plugin = {
718
722
  if (!name) {
719
723
  byName
720
724
  .clear()
725
+
721
726
  byPrefix
722
727
  .clear()
723
728
  } else {
@@ -728,6 +733,7 @@ export const plugin = {
728
733
 
729
734
  byName
730
735
  .delete(name)
736
+
731
737
  byPrefix
732
738
  .delete(prefix)
733
739
  }
@@ -777,9 +783,7 @@ export const plugin = {
777
783
  const relName = getName(rel)
778
784
  const rels = getRels(namespace)
779
785
 
780
- return (
781
- Reflect.get(rels, relName)
782
- )
786
+ return rels[relName] /* Reflect.get(rels, relName) */
783
787
  }
784
788
 
785
789
  /**
@@ -809,7 +813,7 @@ export const plugin = {
809
813
  if (namespace) {
810
814
  const rels = getRels(namespace)
811
815
 
812
- if (!Reflect.has(rels, relName)) {
816
+ if (!(relName in rels)) { /* Reflect.has(rels, relName) */
813
817
  if (!settings.strict) {
814
818
  // lazily create the rel
815
819
  namespace.rel({ name: relName })
@@ -819,10 +823,11 @@ export const plugin = {
819
823
  }
820
824
  }
821
825
 
822
- rel = Reflect.get(rels, relName)
826
+ rel = rels[relName] /* Reflect.get(rels, relName) */
823
827
  } else {
824
828
  // could be globally qualified (e.g. 'self')
825
829
  const { error, value } = relSchema.validate({ name: namespaceName })
830
+
826
831
  if (error) throw error
827
832
 
828
833
  rel = value
@@ -10,14 +10,14 @@ import {
10
10
  const TEMPLATE = /{*}/
11
11
 
12
12
  /**
13
- * A HAL wrapper interface around an entity. Provides an api for adding new links and recursively embedding child
14
- * entities.
13
+ * A HAL wrapper interface around an entity. Provides an api for adding new links and recursively embedding child
14
+ * entities.
15
15
  *
16
- * @param factory
17
- * @param entity
18
- * @param self
19
- * @param root
20
- * @constructor
16
+ * @param factory
17
+ * @param entity
18
+ * @param self
19
+ * @param root
20
+ * @constructor
21
21
  */
22
22
  export class Representation {
23
23
  constructor (factory, entity, link, root = this) {
@@ -88,44 +88,51 @@ export class Representation {
88
88
  }
89
89
 
90
90
  /**
91
- * Adds a namespace to the 'curie' link collection. all curies in a response, top level or nested, should be declared
92
- * in the top level _links collection. a reference '_root' is kept to the top level representation for this purpose
91
+ * Adds a namespace to the 'curie' link collection. all curies in a response, top level or nested, should be declared
92
+ * in the top level _links collection. a reference '_root' is kept to the top level representation for this purpose
93
93
  *
94
- * @param namespace
94
+ * @param namespace
95
95
  */
96
96
  curie (namespace) {
97
97
  if (namespace) {
98
+ const prefix = getPrefix(namespace)
99
+
98
100
  const root = this.getRoot()
99
101
 
100
102
  const namespaces = root.getNamespaces()
101
103
 
102
- const prefix = getPrefix(namespace)
104
+ if (prefix in namespaces) return // if (Reflect.has(namespaces, prefix)) return
103
105
 
104
- if (Reflect.has(namespaces, prefix)) return
105
-
106
- Reflect.set(namespaces, prefix, namespace)
106
+ namespaces[prefix] = namespace // Reflect.set(namespaces, prefix, namespace)
107
107
 
108
108
  const links = root.getLinks()
109
109
 
110
- const curies = Reflect.get(links, 'curies') ?? []
110
+ const curies = links.curies ?? [] // Reflect.get(links, 'curies') ?? []
111
111
 
112
112
  const request = this.getRequest()
113
113
 
114
114
  const namespaceUrl = this.getHalacious().namespaceUrl(request, namespace)
115
115
 
116
+ links.curies = curies.concat({
117
+ name: prefix,
118
+ href: `${namespaceUrl}/{rel}`,
119
+ templated: true
120
+ })
121
+
122
+ /*
116
123
  Reflect.set(links, 'curies', curies.concat({
117
124
  name: prefix,
118
125
  href: `${namespaceUrl}/{rel}`,
119
126
  templated: true
120
- }))
127
+ })) */
121
128
  }
122
129
  }
123
130
 
124
131
  /**
125
- * Adds a custom property to the HAL payload
126
- * @param {String} name the property name
127
- * @param {*} value the property value
128
- * @return {Representation}
132
+ * Adds a custom property to the HAL payload
133
+ * @param {string} name the property name
134
+ * @param {*} value the property value
135
+ * @return {Representation}
129
136
  */
130
137
  prop (name, value) {
131
138
  this.getProps()
@@ -135,9 +142,9 @@ export class Representation {
135
142
  }
136
143
 
137
144
  /**
138
- * Merges an object's properties into the custom properties collection
145
+ * Merges an object's properties into the custom properties collection
139
146
  *
140
- * @param prop
147
+ * @param prop
141
148
  */
142
149
  merge (prop) {
143
150
  const props = this.getProps()
@@ -152,8 +159,8 @@ export class Representation {
152
159
  }
153
160
 
154
161
  /**
155
- * @param {...String || String[]} args properties to ignore
156
- * @return {Representation}
162
+ * @param {...string || string[]} args properties to ignore
163
+ * @return {Representation}
157
164
  */
158
165
  ignore (arg, ...args) {
159
166
  const props = (
@@ -174,9 +181,9 @@ export class Representation {
174
181
  }
175
182
 
176
183
  /**
177
- * Prepares the representation for JSON serialization
184
+ * Prepares the representation for JSON serialization
178
185
  *
179
- * @return {{}}
186
+ * @return {{}}
180
187
  */
181
188
  toJSON () {
182
189
  // initialize the entity
@@ -193,7 +200,7 @@ export class Representation {
193
200
  JSON.stringify(entity, (key, value) => {
194
201
  if (!key) return value
195
202
 
196
- if (!ignore.has(key)) Reflect.set(object, key, value)
203
+ if (!ignore.has(key)) object[key] = value // Reflect.set(object, key, value)
197
204
  })
198
205
 
199
206
  const embedded = this.getEmbedded()
@@ -216,7 +223,7 @@ export class Representation {
216
223
  JSON.stringify(entryValue, (key, value) => {
217
224
  if (!key) return value
218
225
 
219
- if (!ignore.has(key)) Reflect.set(currentValue, key, value)
226
+ if (!ignore.has(key)) currentValue[key] = value // Reflect.set(currentValue, key, value)
220
227
  })
221
228
 
222
229
  return (
@@ -235,11 +242,11 @@ export class Representation {
235
242
  }
236
243
 
237
244
  /**
238
- * Creates a new link and adds it to the _links collection
245
+ * Creates a new link and adds it to the _links collection
239
246
  *
240
- * @param rel
241
- * @param link
242
- * @return {{} || []} the new link
247
+ * @param rel
248
+ * @param link
249
+ * @return {{} || []} the new link
243
250
  */
244
251
  link (relName, link) {
245
252
  const halacious = this.getHalacious()
@@ -250,7 +257,7 @@ export class Representation {
250
257
  const links = this.getLinks()
251
258
 
252
259
  if (Array.isArray(link)) {
253
- if (!Reflect.has(links, key)) Reflect.set(links, key, [])
260
+ if (!(key in links)) links[key] = [] // if (!Reflect.has(links, key)) Reflect.set(links, key, [])
254
261
 
255
262
  return (
256
263
  link.map((href) => this.link(relName, href))
@@ -267,20 +274,27 @@ export class Representation {
267
274
  if (TEMPLATE.test(link.href)) link.templated = true
268
275
 
269
276
  // e.g. 'mco:rel'
277
+ links[key] = (
278
+ key in links
279
+ ? [].concat(links[key], link)
280
+ : link
281
+ )
282
+
283
+ /*
270
284
  Reflect.set(links, key, (
271
285
  Reflect.has(links, key)
272
286
  ? [].concat(Reflect.get(links, key), link)
273
287
  : link
274
- ))
288
+ )) */
275
289
 
276
290
  return link
277
291
  }
278
292
 
279
293
  /**
280
- * Resolves a relative path against the representation's self href
294
+ * Resolves a relative path against the representation's self href
281
295
  *
282
- * @param relativePath
283
- * @return {*}
296
+ * @param relativePath
297
+ * @return {*}
284
298
  */
285
299
  resolve (relativePath) {
286
300
  const href = getHref(getSelf(this.getLinks())) ?? ''
@@ -293,11 +307,12 @@ export class Representation {
293
307
  }
294
308
 
295
309
  /**
296
- * Returns the path to a named route (specified by the plugins.hal.name configuration parameter), expanding any supplied
297
- * path parameters.
298
- * @param {String} routeName the route's name
299
- * @param {{}=} params for expanding templated urls
300
- * @return {*}
310
+ * Returns the path to a named route (specified by the plugins.hal.name configuration parameter), expanding any supplied
311
+ * path parameters
312
+ *
313
+ * @param {string} routeName the route's name
314
+ * @param {{}=} params for expanding templated urls
315
+ * @return {*}
301
316
  */
302
317
  route (routeName, params) {
303
318
  return (
@@ -306,11 +321,12 @@ export class Representation {
306
321
  }
307
322
 
308
323
  /**
309
- * Wraps an entity into a HAL representation and adds it to the _embedded collection
310
- * @param {String} rel the rel name
311
- * @param {String || {}} self an href or link object for the entity
312
- * @param {{} || []} entity an object to wrap
313
- * @return {entity || []}
324
+ * Wraps an entity into a HAL representation and adds it to the _embedded collection
325
+ *
326
+ * @param {string} rel the rel name
327
+ * @param {string || {}} self an href or link object for the entity
328
+ * @param {{} || []} entity an object to wrap
329
+ * @return {entity || []}
314
330
  */
315
331
  embed (relName, self, entity) {
316
332
  const halacious = this.getHalacious()
@@ -323,7 +339,7 @@ export class Representation {
323
339
  const embedded = this.getEmbedded()
324
340
 
325
341
  if (Array.isArray(entity)) {
326
- if (!Reflect.has(embedded, key)) Reflect.set(embedded, key, [])
342
+ if (!(key in embedded)) embedded[key] = [] // if (!Reflect.has(embedded, key)) Reflect.set(embedded, key, [])
327
343
 
328
344
  return (
329
345
  entity.map((entity) => this.embed(relName, self, entity))
@@ -338,22 +354,29 @@ export class Representation {
338
354
 
339
355
  const embed = this.factory.create(entity, self, root)
340
356
 
357
+ embedded[key] = (
358
+ key in embedded
359
+ ? [].concat(embedded[key], embed)
360
+ : embed
361
+ )
362
+
363
+ /*
341
364
  Reflect.set(embedded, key, (
342
365
  Reflect.has(embedded, key)
343
366
  ? [].concat(Reflect.get(embedded, key), embed)
344
367
  : embed
345
- ))
368
+ )) */
346
369
 
347
370
  return embed
348
371
  }
349
372
 
350
373
  /**
351
- * Convenience method for embedding an entity or array of entities
374
+ * Convenience method for embedding an entity or array of entities
352
375
  *
353
- * @param rel
354
- * @param self
355
- * @param arg
356
- * @return {Representation}
376
+ * @param rel
377
+ * @param self
378
+ * @param arg
379
+ * @return {Representation}
357
380
  */
358
381
  embedCollection (rel, self, arg, ...args) {
359
382
  const entities = (
@@ -372,9 +395,10 @@ export class Representation {
372
395
  }
373
396
 
374
397
  /**
375
- * Configures a representation using a configuration object such as those found in route definitions
376
- * @param config
377
- * @param callback
398
+ * Configures a representation using a configuration object such as those found in route definitions
399
+ *
400
+ * @param config
401
+ * @param callback
378
402
  */
379
403
  configure (config, callback) {
380
404
  this.getHalacious().configureRepresentation(config, this, callback)
@@ -1,11 +1,11 @@
1
1
  import Representation from './representation.mjs'
2
2
 
3
3
  /**
4
- * Responsible for creating all hal entities, top level or embedded, needed for a Hapi request
4
+ * Responsible for creating all hal entities, top level or embedded, needed for a Hapi request
5
5
  *
6
- * @param halacious a reference to the plugin api
7
- * @param request a hapi request object
8
- * @constructor
6
+ * @param halacious a reference to the plugin api
7
+ * @param request a hapi request object
8
+ * @constructor
9
9
  */
10
10
  export class RepresentationFactory {
11
11
  constructor (halacious, request) {
@@ -38,12 +38,13 @@ export class RepresentationFactory {
38
38
  }
39
39
 
40
40
  /**
41
- * Creates a new hal representation out of a javascript object
42
- * @param {{}=} entity the entity to wrap with a representation. an empty object is created by default
43
- * @param {String || {}=} self the self href or link object. The request's path is used by default
44
- * @param {Representation} root a pointer to the top level representation for adding curied links
45
- * should be expanded into absolute urls
46
- * @return {Representation}
41
+ * Creates a new hal representation out of a javascript object
42
+ *
43
+ * @param {{}=} entity the entity to wrap with a representation. an empty object is created by default
44
+ * @param {string || {}=} self the self href or link object. The request's path is used by default
45
+ * @param {Representation} root a pointer to the top level representation for adding curied links
46
+ * should be expanded into absolute urls
47
+ * @return {Representation}
47
48
  */
48
49
  create (entity = {}, self = this.getRequestPath(), root) {
49
50
  const halacious = this.getHalacious()
@@ -1,7 +1,7 @@
1
1
  function reduce (accumulator, fieldName) {
2
2
  return (
3
- Reflect.has(accumulator, fieldName)
4
- ? Reflect.get(accumulator, fieldName)
3
+ fieldName in accumulator // Reflect.has(accumulator, fieldName)
4
+ ? accumulator[fieldName] // Reflect.get(accumulator, fieldName)
5
5
  : accumulator
6
6
  )
7
7
  }
@@ -1,12 +1,12 @@
1
1
  import Negotiator from 'negotiator'
2
2
 
3
3
  /**
4
- * Selects the media type based on the request's Accept header and a ranked ordering of configured
5
- * media types
4
+ * Selects the media type based on the request's Accept header and a ranked ordering of configured
5
+ * media types
6
6
  *
7
- * @param request
8
- * @param mediaTypes
9
- * @return {*}
7
+ * @param request
8
+ * @param mediaTypes
9
+ * @return {*}
10
10
  */
11
11
  export default function getMediaType (request, mediaTypes) {
12
12
  return (
@@ -3,12 +3,12 @@ import getFieldValueByPath from './getFieldValueByPath.mjs'
3
3
  const REG = /{([^{}]+)}|([^{}]+)/g
4
4
 
5
5
  /**
6
- * Evaluates and flattens deep expressions (e.g. '/{foo.a.b}') into a single level context object: {'foo.a.b': value}
7
- * so that it may be used by url-template library
6
+ * Evaluates and flattens deep expressions (e.g. '/{foo.a.b}') into a single level context object: {'foo.a.b': value}
7
+ * so that it may be used by url-template library
8
8
  *
9
- * @param template
10
- * @param context
11
- * @return {{}}
9
+ * @param template
10
+ * @param context
11
+ * @return {{}}
12
12
  */
13
13
  export default function getTemplateContext (template, context) {
14
14
  const templateContext = {}
@@ -23,7 +23,8 @@ export default function getTemplateContext (template, context) {
23
23
 
24
24
  if (path) {
25
25
  const fieldValue = getFieldValueByPath(path, context)
26
- if (fieldValue) Reflect.set(templateContext, path, fieldValue.toString())
26
+
27
+ if (fieldValue) templateContext[path] = String(fieldValue) /* Reflect.set(templateContext, path, fieldValue.toString()) */
27
28
  }
28
29
  }
29
30
  }