@qikdev/sdk 1.0.1 → 1.0.4

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.
Files changed (82) hide show
  1. package/README.md +1 -1
  2. package/dist/access.html +16371 -0
  3. package/dist/api.html +1503 -0
  4. package/dist/auth.html +8455 -0
  5. package/dist/cache.html +1135 -0
  6. package/dist/content.html +10067 -0
  7. package/dist/files.html +3415 -0
  8. package/dist/filter.html +4973 -0
  9. package/dist/geo.html +3851 -0
  10. package/dist/global.html +328 -0
  11. package/dist/index.html +96 -0
  12. package/dist/main.js +1100 -1126
  13. package/dist/module-QikStorage-CookieStorageAdapter.html +1177 -0
  14. package/dist/module-QikStorage-LocalStorageAdapter.html +329 -0
  15. package/dist/module-QikStorage-StorageAdapter.html +3457 -0
  16. package/dist/module-QikStorage.html +1445 -0
  17. package/dist/qik.access.js.html +904 -0
  18. package/dist/qik.api.js.html +409 -0
  19. package/dist/{docs/qik.auth.js.html → qik.auth.js.html} +142 -104
  20. package/dist/qik.cache.js.html +125 -0
  21. package/dist/qik.content.js.html +967 -0
  22. package/dist/qik.core.js.html +322 -0
  23. package/dist/qik.files.js.html +249 -0
  24. package/dist/{docs/qik.filter.js.html → qik.filter.js.html} +2 -2
  25. package/dist/qik.geo.js.html +963 -0
  26. package/dist/qik.socket.js.html +451 -0
  27. package/dist/qik.storage.js.html +375 -0
  28. package/dist/qik.system.js.html +117 -0
  29. package/dist/qik.utils.js.html +1542 -0
  30. package/dist/socket.html +2975 -0
  31. package/dist/{docs/styles → styles}/jsdoc.css +2 -2
  32. package/dist/system.html +523 -0
  33. package/package.json +2 -2
  34. package/src/api/qik.access.js +2 -53
  35. package/src/api/qik.socket.js +51 -10
  36. package/src/version.js +1 -1
  37. package/dist/docs/access.html +0 -3826
  38. package/dist/docs/api.html +0 -1356
  39. package/dist/docs/auth.html +0 -2180
  40. package/dist/docs/cache.html +0 -351
  41. package/dist/docs/content.html +0 -2560
  42. package/dist/docs/files.html +0 -779
  43. package/dist/docs/filter.html +0 -1355
  44. package/dist/docs/index.html +0 -96
  45. package/dist/docs/qik.access.js.html +0 -790
  46. package/dist/docs/qik.api.js.html +0 -511
  47. package/dist/docs/qik.cache.js.html +0 -131
  48. package/dist/docs/qik.content.js.html +0 -918
  49. package/dist/docs/qik.core.js.html +0 -301
  50. package/dist/docs/qik.files.js.html +0 -219
  51. package/dist/docs/qik.socket.js.html +0 -416
  52. package/dist/docs/qik.system.js.html +0 -117
  53. package/dist/docs/qik.utils.js.html +0 -1467
  54. package/dist/docs/socket.html +0 -810
  55. package/dist/docs/system.html +0 -197
  56. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Bold.eot +0 -0
  57. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Bold.ttf +0 -0
  58. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Bold.woff +0 -0
  59. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Bold.woff2 +0 -0
  60. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Regular.eot +0 -0
  61. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Regular.ttf +0 -0
  62. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Regular.woff +0 -0
  63. /package/dist/{docs/fonts → fonts}/Montserrat/Montserrat-Regular.woff2 +0 -0
  64. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-light-webfont.eot +0 -0
  65. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-light-webfont.svg +0 -0
  66. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-light-webfont.ttf +0 -0
  67. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-light-webfont.woff +0 -0
  68. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-light-webfont.woff2 +0 -0
  69. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-regular-webfont.eot +0 -0
  70. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-regular-webfont.svg +0 -0
  71. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-regular-webfont.ttf +0 -0
  72. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-regular-webfont.woff +0 -0
  73. /package/dist/{docs/fonts → fonts}/Source-Sans-Pro/sourcesanspro-regular-webfont.woff2 +0 -0
  74. /package/dist/{docs/scripts → scripts}/collapse.js +0 -0
  75. /package/dist/{docs/scripts → scripts}/linenumber.js +0 -0
  76. /package/dist/{docs/scripts → scripts}/nav.js +0 -0
  77. /package/dist/{docs/scripts → scripts}/polyfill.js +0 -0
  78. /package/dist/{docs/scripts → scripts}/prettify/Apache-License-2.0.txt +0 -0
  79. /package/dist/{docs/scripts → scripts}/prettify/lang-css.js +0 -0
  80. /package/dist/{docs/scripts → scripts}/prettify/prettify.js +0 -0
  81. /package/dist/{docs/scripts → scripts}/search.js +0 -0
  82. /package/dist/{docs/styles → styles}/prettify.css +0 -0
@@ -1,918 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
-
5
- <meta charset="utf-8">
6
- <title>qik.content.js - Documentation</title>
7
-
8
-
9
- <script src="scripts/prettify/prettify.js"></script>
10
- <script src="scripts/prettify/lang-css.js"></script>
11
- <!--[if lt IE 9]>
12
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
13
- <![endif]-->
14
- <link type="text/css" rel="stylesheet" href="styles/prettify.css">
15
- <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
16
- <script src="scripts/nav.js" defer></script>
17
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
- </head>
19
- <body>
20
-
21
- <input type="checkbox" id="nav-trigger" class="nav-trigger" />
22
- <label for="nav-trigger" class="navicon-button x">
23
- <div class="navicon"></div>
24
- </label>
25
-
26
- <label for="nav-trigger" class="overlay"></label>
27
-
28
- <nav >
29
-
30
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="access.html">access</a><ul class='methods'><li data-type='method'><a href="access.html#.actionableScopes">actionableScopes</a></li><li data-type='method'><a href="access.html#.allPermissions">allPermissions</a></li><li data-type='method'><a href="access.html#.allPermissionTypes">allPermissionTypes</a></li><li data-type='method'><a href="access.html#.allUserScopes">allUserScopes</a></li><li data-type='method'><a href="access.html#.canCreate">canCreate</a></li><li data-type='method'><a href="access.html#.canDeleteItem">canDeleteItem</a></li><li data-type='method'><a href="access.html#.canEditItem">canEditItem</a></li><li data-type='method'><a href="access.html#.canEraseItem">canEraseItem</a></li><li data-type='method'><a href="access.html#.canKnowOf">canKnowOf</a></li><li data-type='method'><a href="access.html#.canListItem">canListItem</a></li><li data-type='method'><a href="access.html#.canRestoreItem">canRestoreItem</a></li><li data-type='method'><a href="access.html#.canViewItem">canViewItem</a></li><li data-type='method'><a href="access.html#.checkActionAccess">checkActionAccess</a></li><li data-type='method'><a href="access.html#.getAllDescendants">getAllDescendants</a></li><li data-type='method'><a href="access.html#.has">has</a></li><li data-type='method'><a href="access.html#.hashPermissions">hashPermissions</a></li><li data-type='method'><a href="access.html#.isAdministrator">isAdministrator</a></li><li data-type='method'><a href="access.html#.isOwner">isOwner</a></li></ul></li><li><a href="api.html">api</a><ul class='methods'><li data-type='method'><a href="api.html#.delete">delete</a></li><li data-type='method'><a href="api.html#.generateEndpointURL">generateEndpointURL</a></li><li data-type='method'><a href="api.html#.get">get</a></li><li data-type='method'><a href="api.html#.patch">patch</a></li><li data-type='method'><a href="api.html#.post">post</a></li><li data-type='method'><a href="api.html#.put">put</a></li></ul></li><li><a href="auth.html">auth</a><ul class='methods'><li data-type='method'><a href="auth.html#.changeOrganisation">changeOrganisation</a></li><li data-type='method'><a href="auth.html#.ensureValidToken">ensureValidToken</a></li><li data-type='method'><a href="auth.html#.getCurrentToken">getCurrentToken</a></li><li data-type='method'><a href="auth.html#.getCurrentUser">getCurrentUser</a></li><li data-type='method'><a href="auth.html#.impersonate">impersonate</a></li><li data-type='method'><a href="auth.html#.login">login</a></li><li data-type='method'><a href="auth.html#.logout">logout</a></li><li data-type='method'><a href="auth.html#.retrieveUserFromResetToken">retrieveUserFromResetToken</a></li><li data-type='method'><a href="auth.html#.sendResetPasswordRequest">sendResetPasswordRequest</a></li><li data-type='method'><a href="auth.html#.set">set</a></li><li data-type='method'><a href="auth.html#.sync">sync</a></li><li data-type='method'><a href="auth.html#.updateUserWithToken">updateUserWithToken</a></li></ul></li><li><a href="cache.html">cache</a><ul class='methods'><li data-type='method'><a href="cache.html#.get">get</a></li><li data-type='method'><a href="cache.html#.reset">reset</a></li></ul></li><li><a href="content.html">content</a><ul class='methods'><li data-type='method'><a href="content.html#.create">create</a></li><li data-type='method'><a href="content.html#.delete">delete</a></li><li data-type='method'><a href="content.html#.get">get</a></li><li data-type='method'><a href="content.html#.getFromSlug">getFromSlug</a></li><li data-type='method'><a href="content.html#.glossary">glossary</a></li><li data-type='method'><a href="content.html#.list">list</a></li><li data-type='method'><a href="content.html#.patch">patch</a></li><li data-type='method'><a href="content.html#.restore">restore</a></li><li data-type='method'><a href="content.html#.scopeGlossary">scopeGlossary</a></li><li data-type='method'><a href="content.html#.update">update</a></li><li data-type='method'><a href="content.html#.validateField">validateField</a></li><li data-type='method'><a href="content.html#.variables">variables</a></li></ul></li><li><a href="files.html">files</a><ul class='methods'><li data-type='method'><a href="files.html#.downloadUrl">downloadUrl</a></li><li data-type='method'><a href="files.html#.filesize">filesize</a></li><li data-type='method'><a href="files.html#.getBinaryTypeFromMime">getBinaryTypeFromMime</a></li><li data-type='method'><a href="files.html#.mediaUrl">mediaUrl</a></li></ul></li><li><a href="filter.html">filter</a><ul class='methods'><li data-type='method'><a href="filter.html#.activeFilterKeys">activeFilterKeys</a></li><li data-type='method'><a href="filter.html#.activeFilterKeys">activeFilterKeys</a></li><li data-type='method'><a href="filter.html#.activeFilterOperators">activeFilterOperators</a></li><li data-type='method'><a href="filter.html#.activeFilters">activeFilters</a></li><li data-type='method'><a href="filter.html#.activeFilterValues">activeFilterValues</a></li><li data-type='method'><a href="filter.html#.filterChangeString">filterChangeString</a></li><li data-type='method'><a href="filter.html#.isValidFilter">isValidFilter</a></li></ul></li><li><a href="socket.html">socket</a><ul class='methods'><li data-type='method'><a href="socket.html#.channel">channel</a></li><li data-type='method'><a href="socket.html#.close">close</a></li><li data-type='method'><a href="socket.html#.connect">connect</a></li><li data-type='method'><a href="socket.html#.reconnect">reconnect</a></li><li data-type='method'><a href="socket.html#.subscribe">subscribe</a></li><li data-type='method'><a href="socket.html#.unsubscribe">unsubscribe</a></li></ul></li><li><a href="system.html">system</a><ul class='methods'><li data-type='method'><a href="system.html#.countries">countries</a></li></ul></li></ul>
31
- </nav>
32
-
33
- <div id="main">
34
-
35
- <h1 class="page-title">qik.content.js</h1>
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
- <section>
44
- <article>
45
- <pre class="prettyprint source linenums"><code>import axios from 'axios';
46
- import _ from 'lodash';
47
-
48
- ///////////////////////////////////////////////////
49
-
50
- /**
51
- * Creates a new QikContent instance.
52
- * This module provides a number of helper functions for creating and modifying content via the REST API
53
- * @alias content
54
- * @constructor
55
- * @hideconstructor
56
- * @param {QikCore} qik A reference to the parent instance of the QikCore module. This module is usually created by a QikCore instance that passes itself in as the first argument.
57
- */
58
- export default function(qik) {
59
-
60
- if (!qik.api) {
61
- throw new Error(`Can't Instantiate QikContent before QikAPI exists`);
62
- }
63
-
64
- ///////////////////////////////////////////////////
65
-
66
- var service = {
67
- debug: false,
68
- }
69
-
70
-
71
- ///////////////////////////////////////////////////////////////////////////////
72
-
73
- const variables = {}
74
- let inflightVariablesRequest;
75
-
76
-
77
- /**
78
- *
79
- * Retrieves all global variables for the current user. This is often used when running custom code in an action.
80
- * @alias content.variables
81
- * @param {Array} keys Provide specific keys of variables you want to retrieve
82
- * @param {Object} options Additional options when making the request
83
- * @param {Boolean} options.reload Force variables to reload and not be cached. If false will retrieve any variables that are already known from the in memory cache.
84
- * @example
85
- *
86
- * const { OAUTH_CLIENT_ID, OAUTH_KEY } = await sdk.content.variables();
87
- */
88
-
89
- service.variables = async function(keys, options) {
90
- options = options || {};
91
- keys = keys || [];
92
-
93
- var reload = options.refresh || options.reload || !variables.data;
94
- if (reload) {
95
- if(!inflightVariablesRequest) {
96
- inflightVariablesRequest = qik.api.post(`/variables`, {keys});
97
- inflightVariablesRequest.then(resolveRequest, resolveRequest);
98
- function resolveRequest() {
99
- inflightVariablesRequest = null;
100
- }
101
- }
102
-
103
- const { data } = await inflightVariablesRequest
104
- variables.data = data;
105
- }
106
-
107
- return variables.data;
108
- }
109
-
110
- ///////////////////////////////////////////////////
111
-
112
- const glossaryCache = qik.cache.get('user:glossary');
113
-
114
- let inflightGlossaryRequest;
115
-
116
- /**
117
- *
118
- * Retrieves the glossary of all content types visible to the requesting user.
119
- By default this will include all fields, validation, expressions and other configuration
120
- * @alias content.glossary
121
- * @param {Object} options Additional options
122
- * @param {Boolean} options.hash Whether to return the data as a keyed object
123
- allowing for fast selection of specific content types, by default will return as an array
124
- * @param {Boolean} options.reload Force glossary to reload and not be cached.
125
- If false will retrieve content type data from the in memory cache
126
- * @example
127
- *
128
- * const { article, profile } = await sdk.content.glossary({hash:true});
129
- */
130
- service.glossary = async function(options) {
131
- options = options || {};
132
-
133
-
134
- // Check if we already have the glossary cached
135
- let cached = glossaryCache.get('glossary');
136
- var reload = options.refresh || options.reload || !cached;
137
-
138
- // If we are forcing a reload
139
- if (reload) {
140
- if(!inflightGlossaryRequest) {
141
- inflightGlossaryRequest = qik.api.get(`/glossary`, {cache:false});
142
- inflightGlossaryRequest.then(resolveRequest, resolveRequest);
143
- function resolveRequest() {
144
- inflightGlossaryRequest = null;
145
- }
146
- }
147
-
148
- // Now we have the glossary
149
- const { data } = await inflightGlossaryRequest
150
-
151
- // Cache it for later
152
- glossaryCache.set('glossary', data);
153
- cached = data;
154
- qik.dispatch('glossary', data)
155
- }
156
-
157
-
158
- if (options.hash) {
159
- var result = qik.utils.hash(cached, 'key');
160
- return result;
161
- }
162
-
163
- if (options.hex) {
164
- var result = qik.utils.hash(cached, 'hex');
165
- return result;
166
- }
167
-
168
- return cached;
169
- }
170
-
171
-
172
-
173
- ///////////////////////////////////////////////////
174
-
175
-
176
-
177
- const scopeGlossary = {}
178
-
179
- let inflightScopeGlossaryRequest;
180
-
181
- /**
182
- *
183
- * Retrieves the scope glossary of all scopes the user can know about. This helps to convert a scope id into a human readable title.
184
- * @alias content.scopeGlossary
185
- * @param {Object} options Additional options
186
- * @param {Boolean} options.hash Whether to return the data as a keyed object with each scopes _id as the key
187
- allowing for fast selection of specific scopes, by default will return a structured tree
188
- * @param {Boolean} options.reload Force the glossary to reload and not be cached.
189
- If false will retrieve content type data from the in memory cache
190
- * @example
191
- *
192
- * const scopes = await sdk.content.scopeGlossary();
193
- */
194
-
195
- service.scopeGlossary = async function(options) {
196
- options = options || {};
197
-
198
- var reload = options.refresh || options.reload || !scopeGlossary.data;
199
-
200
-
201
- if (reload) {
202
-
203
- if(!inflightScopeGlossaryRequest) {
204
- inflightScopeGlossaryRequest = qik.api.get(`/scope/glossary`, {cache:false});
205
- inflightScopeGlossaryRequest.then(resolveRequest, resolveRequest);
206
- function resolveRequest() {
207
- inflightScopeGlossaryRequest = null;
208
- }
209
- }
210
-
211
- const { data } = await inflightScopeGlossaryRequest
212
- scopeGlossary.data = data;
213
- }
214
-
215
- if (options.hash) {
216
- var result = qik.utils.hash(scopeGlossary.data, '_id');
217
- return result;
218
- }
219
-
220
- return scopeGlossary.data;
221
- }
222
-
223
-
224
-
225
-
226
- ///////////////////////////////////////////////////
227
-
228
- /**
229
- *
230
- * Retrieves all available filter comparators for each data type
231
- * @alias content.comparators
232
- * @param {Object} options Additional options
233
- * @param {Boolean} options.reload Ignore any locally cached data
234
- * @example
235
- * const {hash, available, types} = await sdk.content.comparators();
236
- * console.log(available) // {boolean:[{title:'Is equal to', operator:'equal'...}]}
237
- * console.log(hash) // {equal:[{title:'Is equal to', operator:'equal'...}]}
238
- */
239
-
240
-
241
- const comparators = {}
242
-
243
- service.comparators = async function(options) {
244
- options = options || {};
245
-
246
- var reload = options.reload || options.refresh || !comparators.data;
247
-
248
- if (reload) {
249
- const { data } = await qik.api.get(`/system/comparators`);
250
-
251
-
252
- /////////////////////////////
253
-
254
- data.available = {};
255
-
256
- Object.entries(data.types).forEach(([key, value]) => {
257
- data.available[key] = value.map(function(comparatorKey) {
258
- return data.hash[comparatorKey];
259
- })
260
- });
261
-
262
-
263
- /////////////////////////////
264
-
265
- comparators.data = data;
266
- }
267
-
268
-
269
-
270
- // if (options.hash) {
271
- // var result = qik.utils.hash(comparators.data, 'key');
272
- // return result;
273
- // }
274
-
275
- return comparators.data;
276
- }
277
-
278
- ///////////////////////////////////////////////////
279
-
280
-
281
- function getLimits(fieldDefinition) {
282
-
283
- var { minimum, maximum } = fieldDefinition;
284
- minimum = qik.utils.parseInt(minimum);
285
- minimum = Math.max(minimum, 0);
286
-
287
- maximum = qik.utils.parseInt(maximum);
288
- maximum = Math.max(maximum, 0); // cant be less than 0
289
- if (maximum == 0) {
290
- //Unlimited maximum
291
- } else {
292
- maximum = Math.max(maximum, minimum); // cant be less than the minimum
293
- }
294
- return { minimum, maximum };
295
- }
296
-
297
-
298
-
299
-
300
- service.meetsValidationRequirements = function(input, fieldType, validationCriteria) {
301
-
302
- if (validationCriteria.minLength) {
303
- var stringLength = String(input).length;
304
- if (stringLength &lt; validationCriteria.minLength) {
305
- return `Must be at least ${validationCriteria.minLength} characters`;
306
-
307
- }
308
- }
309
-
310
- if (validationCriteria.maxLength) {
311
- var stringLength = String(input).length;
312
- if (stringLength > validationCriteria.maxLength) {
313
- return `Can be no more than ${validationCriteria.maxLength} characters`;
314
-
315
- }
316
- }
317
-
318
- if (validationCriteria.minValue) {
319
- if(input !== undefined) {
320
-
321
-
322
- var number = Number(input);
323
- if (number &lt; validationCriteria.minValue) {
324
- return `Must be at least ${validationCriteria.minValue}`;
325
-
326
- }
327
- }
328
- }
329
-
330
- if (validationCriteria.maxValue) {
331
- if(input !== undefined) {
332
-
333
-
334
- var number = Number(input);
335
- if (number > validationCriteria.maxValue) {
336
- return `Can be no greater than ${validationCriteria.maxValue}`;
337
-
338
- }
339
- }
340
- }
341
- }
342
-
343
- /**
344
- * Checks if a certain input validates against a field definition
345
- * @alias content.validateField
346
- * @param {Any} input The input to validate
347
- * @param {Object} fieldDefinition The field to validate against
348
- * @param {Object} options Additional options when calling the function
349
- * @example
350
- * const validationResult = await sdk.content.validateField('Johnny Bobbins', {title:'Name', key:'firstName', type:'string', minimum:1, maximum:1, ...});
351
- * console.log(validationResult)
352
- * // Results in { valid:true }
353
- *
354
- * const validationResult = await sdk.content.validateField('Johnny Bobbins', {title:'Number', key:'number', type:'integer', minimum:1, maximum:1, ...});
355
- * console.log(validationResult)
356
- * // Results in { valid:false, status:400, message:'Invalid number input for field' }
357
- */
358
-
359
- service.validateField = function(input, fieldDefinition, options) {
360
- options = options || {}
361
-
362
- var fieldType = fieldDefinition.type || 'string';
363
- var { minimum, maximum } = getLimits(fieldDefinition);
364
-
365
- var isObject = (fieldDefinition.type == 'group' &amp;&amp; fieldDefinition.asObject);
366
- var singleValue = isObject ? minimum === maximum &amp;&amp; maximum === 1 : maximum === 1;
367
- var multiValue = !singleValue;
368
-
369
- //////////////////
370
-
371
- var isNumeric;
372
- switch (fieldType) {
373
- case 'integer':
374
- case 'decimal':
375
- case 'number':
376
- case 'float':
377
- isNumeric = true;
378
- break;
379
- }
380
-
381
- //////////////////
382
-
383
- var inputWasProvided = qik.utils.exists(input);
384
-
385
- //If an answer is required
386
- if (minimum) {
387
- //but none was provided
388
- if (!inputWasProvided) {
389
- //Throw an error
390
- return {
391
- valid: false,
392
- message: `${fieldDefinition.title} is a required field`,
393
- status: 400,
394
- }
395
- }
396
- } else {
397
- //No answer is needed and none was provided
398
- if (!inputWasProvided) {
399
- return {
400
- valid: true,
401
- }
402
- }
403
- }
404
-
405
- //////////////////
406
-
407
-
408
- //If we are requiring multiple values
409
- if (multiValue) {
410
-
411
- //But the input is not an array
412
- if (!Array.isArray(input)) {
413
-
414
- if (minimum) {
415
- return {
416
- valid: false,
417
- message: `${fieldDefinition.title} requires at least ${minimum} values`,
418
- status: 400,
419
- }
420
- } else {
421
-
422
- console.log('NOT PROVIDED AS ARRAY', fieldDefinition.title, input)
423
- return {
424
- valid: false,
425
- message: `${fieldDefinition.title} must be provided as an array`,
426
- status: 400,
427
- }
428
- }
429
- }
430
-
431
-
432
-
433
- ////////////////////////////
434
-
435
- var compacted = input;
436
-
437
- compacted = compacted.filter(function(v) {
438
- //Check if we care about this value
439
- var empty = v === undefined || v === null || v === '';
440
- return !empty;
441
- })
442
-
443
-
444
- // console.log('COMPACTED', input.length, compacted.length);
445
-
446
- ////////////////////////////
447
-
448
- //We need an exact number of answers
449
- if (minimum == maximum) {
450
-
451
- //But we don't have the number of answers needed
452
- if (minimum &amp;&amp; (compacted.length != minimum)) {
453
- return {
454
- valid: false,
455
- message: `${fieldDefinition.title} requires exactly ${maximum} values`,
456
- status: 400,
457
- }
458
- }
459
- }
460
-
461
- //We have too many answers
462
- if (maximum &amp;&amp; (compacted.length > maximum)) {
463
-
464
- return {
465
- valid: false,
466
- message: `${fieldDefinition.title} requires less than ${maximum+1} values`,
467
- status: 400,
468
- }
469
- }
470
-
471
- //We don't have enough answers
472
- if (compacted.length &lt; minimum) {
473
-
474
- // console.log(fieldDefinition.title, 'INPUT', input.length, minimum, input, compacted)
475
-
476
- return {
477
- valid: false,
478
- message: `${fieldDefinition.title} requires at least ${minimum} values`,
479
- status: 400,
480
- }
481
- }
482
-
483
- ////////////////////////////
484
-
485
- var foundBadEntry;
486
-
487
- //Find any bad values
488
- var badEntry = compacted.find(function(val) {
489
- var valueFieldType = fieldType;
490
-
491
- if (fieldDefinition.type === 'group') {
492
- valueFieldType = 'object';
493
- }
494
-
495
- if (fieldDefinition.type === 'reference' &amp;&amp; fieldDefinition.widget === 'form') {
496
- valueFieldType = 'object';
497
- }
498
-
499
- var isValid = qik.utils.isValidValue(val, valueFieldType, options.strict);
500
-
501
- ////////////////////////////
502
-
503
- if (!isValid) {
504
- foundBadEntry = true;
505
- return true;
506
- }
507
-
508
- ////////////////////////////
509
-
510
- //Is there additional validation requirements
511
- if (fieldDefinition.validation) {
512
- var additionalValidationErrors = service.meetsValidationRequirements(val, fieldType, fieldDefinition.validation)
513
- if (additionalValidationErrors) {
514
- foundBadEntry = true;
515
- return true;
516
- }
517
- }
518
- })
519
-
520
- if (foundBadEntry) {
521
- let badValueMessage = `Invalid input for ${fieldDefinition.title}`;
522
- return {
523
- valid: false,
524
- message: badValueMessage,
525
- status: 400,
526
- }
527
- }
528
-
529
- } else {
530
-
531
- var dataType = fieldType;
532
- var widgetType = fieldDefinition.widget;
533
- if (dataType === 'group') {
534
- dataType = 'object';
535
- }
536
-
537
- if (dataType === 'reference' &amp;&amp; widgetType === 'form') {
538
- dataType = 'object';
539
- }
540
-
541
-
542
- //////////////////
543
-
544
- var cleanedValue = service.getCleanedValue(input, dataType, options);
545
-
546
-
547
- //////////////////
548
-
549
- var isValidValue = qik.utils.isValidValue(cleanedValue, dataType, options.strict);
550
-
551
- //////////////////
552
-
553
- //Invalid input
554
- if (!isValidValue) {
555
- return {
556
- valid: false,
557
- message: `Single value '${input}' is not a valid ${dataType} for ${fieldDefinition.title}`,
558
- criteria: {
559
- isValidValue,
560
- cleanedValue,
561
- fieldType,
562
- options,
563
- },
564
- status: 400,
565
- }
566
- }
567
-
568
- //Is there additional validation requirements
569
- let hasAdditionalValidation = false;
570
-
571
- const validation = {}
572
- if(String(fieldDefinition.minValue)) { validation.minValue = parseInt(fieldDefinition.minValue); hasAdditionalValidation = true }
573
- if(String(fieldDefinition.maxValue)) { validation.maxValue = parseInt(fieldDefinition.maxValue); hasAdditionalValidation = true }
574
- if(String(fieldDefinition.minLength)) { validation.minLength = parseInt(fieldDefinition.minLength); hasAdditionalValidation = true }
575
- if(String(fieldDefinition.maxLength)) { validation.maxLength = parseInt(fieldDefinition.maxLength); hasAdditionalValidation = true }
576
-
577
-
578
- if (hasAdditionalValidation) {
579
- const validationCriteria = Object.assign({}, validation, fieldDefinition.validation || {});
580
- var additionalValidationError = service.meetsValidationRequirements(input, fieldType, validationCriteria)
581
- if (additionalValidationError) {
582
- return {
583
- valid: false,
584
- message: additionalValidationError,
585
- status: 400,
586
- };
587
- }
588
- }
589
- }
590
-
591
- //////////////////
592
-
593
- return {
594
- valid: true,
595
- }
596
-
597
- }
598
-
599
- service.getCleanedValue = function(input, dataType, options) {
600
- switch (dataType) {
601
- case 'number':
602
- case 'float':
603
- case 'decimal':
604
- if (!qik.utils.exists(input)) {
605
- return undefined;
606
- } else {
607
- return Number(input);
608
- }
609
- break;
610
- case 'integer':
611
- if (!qik.utils.exists(input)) {
612
- return undefined;
613
- } else {
614
- return parseInt(input);
615
- }
616
- break;
617
- case 'boolean':
618
- if (!qik.utils.exists(input)) {
619
- return undefined;
620
- }
621
- return qik.utils.parseBoolean(input);
622
- break;
623
- case 'email':
624
- return options.strict ? input : String(input).toLowerCase();
625
- break;
626
- case 'reference':
627
- return options.strict ? input : qik.utils.id(input);
628
- break;
629
- default:
630
- return options.strict ? input : qik.utils.cleanValue(input, dataType, options);
631
- break
632
- }
633
-
634
- return input;
635
- }
636
-
637
- ///////////////////////////////////////////////////
638
-
639
- /**
640
- *
641
- * Retrieves a list of records matching the provided criteria
642
- * @alias content.list
643
- * @param {String} type The type or definition of records we want to retrieve
644
- * @param {Object} options The options for our query
645
- * @param {String} options.search Freeform text keywords
646
- * @param {Object} options.sort How to sort the results
647
- * @param {String} options.sort.key Which key to sort on
648
- * @param {String} options.sort.direction Which direction to sort on
649
- * @param {Object} options.sort.type What type of data is being sorted
650
- * @param {Object} options.page Page configuration
651
- * @param {Number} options.page.size Page size
652
- * @param {Number} options.page.index Page index
653
- * @param {Object} options.filter How to filter the results
654
- * @example
655
- *
656
- *
657
- * sdk.content.list('profile', {
658
- * search:'Jim',
659
- * page:{
660
- * size:50,
661
- * index:2,
662
- * },
663
- * sort:{
664
- * key:'age',
665
- * direction:'asc',
666
- * type:'integer',
667
- * },
668
- * filter:{
669
- * operator:'and',
670
- * filters:[{
671
- * key:'age',
672
- * comparator:'>',
673
- * value:5,
674
- * }],
675
- * },
676
- * })
677
- */
678
-
679
- service.list = async function(type, options, advanced) {
680
-
681
-
682
-
683
-
684
- if (!advanced) {
685
- advanced = {};
686
- }
687
-
688
- const remoteURL = advanced.remoteURL || `/content/${type}/list`;
689
-
690
- if (advanced.cancellable) {
691
-
692
- if (!advanced.config) {
693
- advanced.config = {}
694
- }
695
-
696
- //Create a cancel token
697
- const CancelToken = qik.api.CancelToken;
698
- const source = CancelToken.source();
699
- advanced.config.cancelToken = source.token;
700
-
701
-
702
- const promise = qik.api.post(remoteURL, options, advanced.config);
703
- return {
704
- promise,
705
- cancel(message) {
706
- source.cancel(message || 'Operation canceled by the user.');
707
- },
708
- }
709
-
710
- } else {
711
- const { data } = await qik.api.post(remoteURL, options, advanced.config);
712
- return data;
713
- }
714
-
715
-
716
-
717
-
718
-
719
- }
720
-
721
- ///////////////////////////////////////////////////
722
-
723
- /**
724
- *
725
- * Create an item
726
- * @alias content.create
727
- * @param {String} type The type or definition of the record we want to create
728
- * @param {Object} input The data for our new record
729
- * @example
730
- *
731
- *
732
- * const result = await sdk.content.create('profile', {
733
- * firstName:'Mickey',
734
- * lastName:'Mouse',
735
- * gender:'male',
736
- * meta:{
737
- * scopes:['61eca4746971e75c1fc670cf'],
738
- * }
739
- * })
740
- */
741
-
742
- service.create = async function(type, input) {
743
-
744
- const dataModel = {...input};
745
-
746
- // Sanitize down to just ids before we send
747
- if(dataModel.meta?.scopes) {
748
- dataModel.meta.scopes = qik.utils.ids(dataModel.meta.scopes);
749
- }
750
-
751
-
752
- const { data } = await qik.api.post(`/content/${type}/create`, dataModel);
753
- return data;
754
- }
755
-
756
- /**
757
- *
758
- * Update an item, Only fields the user has permission to view will be returned
759
- * @alias content.update
760
- * @param {String} id The id of the record we want to update
761
- * @param {Object} input The data to update
762
- * @example
763
- *
764
- * const result = await sdk.content.update('61eca4746971e75c1fc670cd', {
765
- * firstName:'Minnie',
766
- * lastName:'Mouse',
767
- * gender:'female',
768
- * meta:{
769
- * scopes:['61eca4746971e75c1fc670cd'],
770
- * }
771
- * })
772
- */
773
- service.update = async function(id, input) {
774
- id = qik.utils.id(id);
775
-
776
- const dataModel = {...input};
777
-
778
- // Sanitize down to just ids before we send
779
- if(dataModel.meta?.scopes) {
780
- dataModel.meta.scopes = qik.utils.ids(dataModel.meta.scopes);
781
- }
782
-
783
-
784
- const { data } = await qik.api.put(`/content/${id}`, dataModel);
785
- return data;
786
- }
787
-
788
- /**
789
- *
790
- * Partially update and patch an item, Only fields the user has permission to edit will be updated
791
- * @alias content.patch
792
- * @param {String} id The id of the record we want to update
793
- * @param {Object} input The data to update, this will be merged with existing data
794
- * @example
795
- *
796
- * const result = await sdk.content.patch('61eca4746971e75c1fc670cd', {
797
- * firstName:'Mickey',
798
- * gender:'male',
799
- * })
800
- */
801
- service.patch = async function(id, input) {
802
- id = qik.utils.id(id);
803
-
804
- const dataModel = {...input};
805
-
806
- // Sanitize down to just ids before we send
807
- if(dataModel.meta?.scopes) {
808
- dataModel.meta.scopes = qik.utils.ids(dataModel.meta.scopes);
809
- }
810
-
811
-
812
- const { data } = await qik.api.patch(`/content/${id}`, dataModel);
813
- return data;
814
- }
815
-
816
- /**
817
- *
818
- * Get an item from the database, Only fields the user has permission to view will be returned
819
- * @alias content.get
820
- * @param {String} id The id of the record we want to update
821
- * @example
822
- *
823
- * const result = await sdk.content.get('61eca4746971e75c1fc670cd')
824
- */
825
- service.get = async function(id) {
826
- id = qik.utils.id(id);
827
- const { data } = await qik.api.get(`/content/${id}`);
828
- return data;
829
- }
830
-
831
- service.getFromID = async function(id) {
832
- return service.get(id);
833
- }
834
-
835
- /**
836
- *
837
- * Delete an item from the database
838
- * @alias content.delete
839
- * @param {String} id The id of the record we want to delete
840
- * @example
841
- * const result = await sdk.content.delete('61eca4746971e75c1fc670cd')
842
- */
843
- service.delete = async function(id, input) {
844
- id = qik.utils.id(id);
845
- const { data } = await qik.api.delete(`/content/${id}`, input);
846
- return data;
847
- }
848
-
849
- /**
850
- *
851
- * Restore a deleted item from the database
852
- * @alias content.restore
853
- * @param {String} id The id of the record we want to restore
854
- * @example
855
- * const result = await sdk.content.restore('61eca4746971e75c1fc670cd')
856
- */
857
- service.restore = async function(id, input) {
858
- id = qik.utils.id(id);
859
- const { data } = await qik.api.get(`/content/${id}/restore`, input);
860
- return data;
861
- }
862
-
863
-
864
- /**
865
- *
866
- * Retrieve an item from the database by providing it's 'slug'
867
- * @alias content.getFromSlug
868
- * @param {String} slug The slug id of the record we want to retrieve
869
- * it must be provided as either `(type):(slug)`` or `(definition):(slug)`
870
- * (if unsure use the `meta.abs` absolute slug property of the item you are wanting to retrieve).
871
- * @example
872
- * const result = await sdk.content.getFromSlug('article:how-to-get-started')
873
- * const result = await sdk.content.getFromSlug('car:toyota-landcruiser')
874
- * const result = await sdk.content.getFromSlug('article:toyota-landcruiser')
875
- */
876
- service.getFromSlug = async function(slug) {
877
- const { data } = await qik.api.get(`/content/slug/${slug}`);
878
- return data;
879
- }
880
-
881
- // service.getFromSlug = async function(type, slug) {
882
- // const { data } = await qik.api.get(`/content/${type}/slug/${slug}`);
883
- // return data;
884
- // }
885
-
886
- // service.getFromExternalID = async function(type, externalID) {
887
- // const { data } = await qik.api.get(`/content/${type}/external/${externalID}`);
888
- // return data;
889
- // }
890
-
891
-
892
- return service;
893
-
894
- }</code></pre>
895
- </article>
896
- </section>
897
-
898
-
899
-
900
-
901
-
902
-
903
- </div>
904
-
905
- <br class="clear">
906
-
907
- <footer>
908
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.11</a> on Thu Sep 28 2023 07:24:39 GMT+1000 (Australian Eastern Standard Time) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
909
- </footer>
910
-
911
- <script>prettyPrint();</script>
912
- <script src="scripts/polyfill.js"></script>
913
- <script src="scripts/linenumber.js"></script>
914
-
915
-
916
-
917
- </body>
918
- </html>