crelte 0.5.7 → 0.5.8

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 +1 @@
1
- {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../../src/server/queries/queries.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAI9C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAqB1C,wBAAsB,eAAe,CACpC,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,YAAY,GAClB,OAAO,CAAC,IAAI,CAAC,CAgEf"}
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../../src/server/queries/queries.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,oBAAoB,CAAC;AAI9C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAwB1C,wBAAsB,eAAe,CACpC,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,GAAG,EACR,MAAM,EAAE,YAAY,GAClB,OAAO,CAAC,IAAI,CAAC,CAwEf"}
@@ -19,6 +19,8 @@ export async function initQueryRoutes(platform, mod, router) {
19
19
  jsFile: null,
20
20
  vars: null,
21
21
  cacheIfFn: null,
22
+ preventCaching: false,
23
+ transformFn: null,
22
24
  };
23
25
  preRoutes.set(name, preRoute);
24
26
  }
@@ -38,6 +40,12 @@ export async function initQueryRoutes(platform, mod, router) {
38
40
  if (mts.caching) {
39
41
  preRoute.cacheIfFn = parseCaching(mts.caching);
40
42
  }
43
+ else if (typeof mts.caching === 'boolean') {
44
+ preRoute.preventCaching = true;
45
+ }
46
+ if (mts.transform) {
47
+ preRoute.transformFn = parseTransform(mts.transform);
48
+ }
41
49
  }
42
50
  const caching = new QueriesCaching(platform, router, {
43
51
  debug: debugCaching,
@@ -45,7 +53,7 @@ export async function initQueryRoutes(platform, mod, router) {
45
53
  for (const [name, pr] of preRoutes.entries()) {
46
54
  if (!pr.query)
47
55
  throw new Error(`no .graphql file for query ${name}`);
48
- const route = new QueryRoute(name, pr.query, pr.vars, pr.cacheIfFn);
56
+ const route = new QueryRoute(name, pr.query, pr);
49
57
  router.post('/queries/' + route.name, async (csr) => route.handle(caching, csr));
50
58
  }
51
59
  }
@@ -68,3 +76,8 @@ function parseCaching(caching) {
68
76
  throw new Error('caching should be a function or a boolean');
69
77
  return caching;
70
78
  }
79
+ function parseTransform(transform) {
80
+ if (typeof transform !== 'function')
81
+ throw new Error('transform should be a function');
82
+ return transform;
83
+ }
@@ -3,12 +3,20 @@ import QueriesCaching from './QueriesCaching.js';
3
3
  import { QueryVar } from '../../queries/vars.js';
4
4
  import ServerRouter from '../ServerRouter.js';
5
5
  export type CacheIfFn = (response: any, vars: Record<string, any>) => boolean;
6
+ export type TransformFn = (response: any, vars: Record<string, any>) => void | any | Promise<void | any>;
7
+ export type QueryRouteArgs = {
8
+ vars: Record<string, QueryVar> | null;
9
+ cacheIfFn: CacheIfFn | null;
10
+ preventCaching: boolean;
11
+ transformFn: TransformFn | null;
12
+ };
6
13
  export declare class QueryRoute {
7
14
  name: string;
8
15
  query: string;
9
16
  vars: Record<string, QueryVar> | null;
10
17
  cacheIfFn: CacheIfFn | null;
11
- constructor(name: string, query: string, vars: Record<string, QueryVar> | null, cacheIfFn: CacheIfFn | null);
18
+ transformFn: TransformFn | null;
19
+ constructor(name: string, query: string, args: QueryRouteArgs);
12
20
  private fillEntryDefaults;
13
21
  private fillGlobalDefaults;
14
22
  /**
@@ -21,6 +29,7 @@ export declare class QueryRoute {
21
29
  * else just returns all vars
22
30
  */
23
31
  validateVars(vars: any, cs: ServerRouter): Record<string, any>;
32
+ private transform;
24
33
  handle(caching: QueriesCaching, csr: CrelteServerRequest): Promise<Response>;
25
34
  }
26
35
  export declare function newError(e: any, status: number): Response;
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../../src/server/queries/routes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B,GACtB,OAAO,CAAC,QAAQ,CAAC;CAkHpB;AAED,wBAAgB,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,QAAQ,CAEzD"}
@@ -7,15 +7,24 @@ export class QueryRoute {
7
7
  query;
8
8
  vars;
9
9
  cacheIfFn;
10
- constructor(name, query, vars, cacheIfFn) {
11
- if (cacheIfFn && !vars)
10
+ transformFn;
11
+ constructor(name, query, args) {
12
+ if (args.cacheIfFn && !vars)
12
13
  throw new Error('queryRoute: ' +
13
14
  name +
14
- ' cannot have cacheIfFn if there are no vars defined');
15
+ ' cannot have caching function if there are no ' +
16
+ 'variables defined');
15
17
  this.name = name;
16
18
  this.query = query;
17
- this.vars = vars;
18
- this.cacheIfFn = cacheIfFn;
19
+ this.vars = args.vars;
20
+ this.cacheIfFn = args.cacheIfFn;
21
+ this.transformFn = args.transformFn;
22
+ if (args.preventCaching) {
23
+ if (this.cacheIfFn)
24
+ throw new Error('unreachable');
25
+ // prevent filling defaults
26
+ return;
27
+ }
19
28
  // add default vars and cacheIfFn if we know the route
20
29
  if (this.name === 'entry')
21
30
  this.fillEntryDefaults();
@@ -74,6 +83,13 @@ export class QueryRoute {
74
83
  }
75
84
  return nVars;
76
85
  }
86
+ async transform(jsonResp, vars) {
87
+ if (!this.transformFn || !jsonResp.data)
88
+ return;
89
+ const transformed = await this.transformFn(jsonResp.data, vars);
90
+ if (typeof transformed !== 'undefined')
91
+ jsonResp.data = transformed;
92
+ }
77
93
  async handle(caching, csr) {
78
94
  let vars;
79
95
  try {
@@ -155,6 +171,7 @@ export class QueryRoute {
155
171
  jsonResp = await resp.json();
156
172
  if (!jsonResp || typeof jsonResp !== 'object')
157
173
  throw new Error('invalid json response');
174
+ await this.transform(jsonResp, vars);
158
175
  }
159
176
  catch (e) {
160
177
  return newError(e, 500);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "crelte",
3
- "version": "0.5.7",
3
+ "version": "0.5.8",
4
4
  "author": "Crelte <support@crelte.com>",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  import ServerRouter from '../ServerRouter.js';
2
2
  import QueriesCaching from './QueriesCaching.js';
3
- import { CacheIfFn, QueryRoute } from './routes.js';
3
+ import { CacheIfFn, QueryRoute, TransformFn } from './routes.js';
4
4
  import { isQueryVar, QueryVar } from '../../queries/vars.js';
5
5
  import { Platform } from '../platform.js';
6
6
 
@@ -10,8 +10,9 @@ type ModQuery = {
10
10
  };
11
11
 
12
12
  type ModTs = {
13
- variables: any;
13
+ variables?: any;
14
14
  caching?: any;
15
+ transform?: any;
15
16
  };
16
17
 
17
18
  type ModQueries = Record<string, ModQuery | ModTs>;
@@ -21,6 +22,8 @@ type PreRoute = {
21
22
  jsFile: string | null;
22
23
  vars: Record<string, QueryVar> | null;
23
24
  cacheIfFn: CacheIfFn | null;
25
+ preventCaching: boolean;
26
+ transformFn: TransformFn | null;
24
27
  };
25
28
 
26
29
  export async function initQueryRoutes(
@@ -51,6 +54,8 @@ export async function initQueryRoutes(
51
54
  jsFile: null,
52
55
  vars: null,
53
56
  cacheIfFn: null,
57
+ preventCaching: false,
58
+ transformFn: null,
54
59
  };
55
60
  preRoutes.set(name, preRoute);
56
61
  }
@@ -75,6 +80,12 @@ export async function initQueryRoutes(
75
80
 
76
81
  if (mts.caching) {
77
82
  preRoute.cacheIfFn = parseCaching(mts.caching);
83
+ } else if (typeof mts.caching === 'boolean') {
84
+ preRoute.preventCaching = true;
85
+ }
86
+
87
+ if (mts.transform) {
88
+ preRoute.transformFn = parseTransform(mts.transform);
78
89
  }
79
90
  }
80
91
 
@@ -85,7 +96,7 @@ export async function initQueryRoutes(
85
96
  for (const [name, pr] of preRoutes.entries()) {
86
97
  if (!pr.query) throw new Error(`no .graphql file for query ${name}`);
87
98
 
88
- const route = new QueryRoute(name, pr.query, pr.vars, pr.cacheIfFn);
99
+ const route = new QueryRoute(name, pr.query, pr);
89
100
 
90
101
  router.post('/queries/' + route.name, async csr =>
91
102
  route.handle(caching, csr),
@@ -119,3 +130,10 @@ function parseCaching(caching: any): CacheIfFn {
119
130
 
120
131
  return caching;
121
132
  }
133
+
134
+ function parseTransform(transform: any): TransformFn {
135
+ if (typeof transform !== 'function')
136
+ throw new Error('transform should be a function');
137
+
138
+ return transform;
139
+ }
@@ -7,30 +7,50 @@ import { calcKey } from '../../ssr/index.js';
7
7
 
8
8
  export type CacheIfFn = (response: any, vars: Record<string, any>) => boolean;
9
9
 
10
+ /// Anything other than returning undefined will replace the response
11
+ //
12
+ // Note that even if you return undefined since the response is by reference
13
+ // you're modifications will be reflected
14
+ export type TransformFn = (
15
+ response: any,
16
+ vars: Record<string, any>,
17
+ ) => void | any | Promise<void | any>;
18
+
19
+ export type QueryRouteArgs = {
20
+ vars: Record<string, QueryVar> | null;
21
+ cacheIfFn: CacheIfFn | null;
22
+ preventCaching: boolean;
23
+ transformFn: TransformFn | null;
24
+ };
25
+
10
26
  // only internal
11
27
  export class QueryRoute {
12
28
  name: string;
13
29
  query: string;
14
30
  vars: Record<string, QueryVar> | null;
15
31
  cacheIfFn: CacheIfFn | null;
32
+ transformFn: TransformFn | null;
16
33
 
17
- constructor(
18
- name: string,
19
- query: string,
20
- vars: Record<string, QueryVar> | null,
21
- cacheIfFn: CacheIfFn | null,
22
- ) {
23
- if (cacheIfFn && !vars)
34
+ constructor(name: string, query: string, args: QueryRouteArgs) {
35
+ if (args.cacheIfFn && !vars)
24
36
  throw new Error(
25
37
  'queryRoute: ' +
26
38
  name +
27
- ' cannot have cacheIfFn if there are no vars defined',
39
+ ' cannot have caching function if there are no ' +
40
+ 'variables defined',
28
41
  );
29
42
 
30
43
  this.name = name;
31
44
  this.query = query;
32
- this.vars = vars;
33
- this.cacheIfFn = cacheIfFn;
45
+ this.vars = args.vars;
46
+ this.cacheIfFn = args.cacheIfFn;
47
+ this.transformFn = args.transformFn;
48
+
49
+ if (args.preventCaching) {
50
+ if (this.cacheIfFn) throw new Error('unreachable');
51
+ // prevent filling defaults
52
+ return;
53
+ }
34
54
 
35
55
  // add default vars and cacheIfFn if we know the route
36
56
  if (this.name === 'entry') this.fillEntryDefaults();
@@ -96,6 +116,16 @@ export class QueryRoute {
96
116
  return nVars;
97
117
  }
98
118
 
119
+ private async transform(
120
+ jsonResp: Record<string, any>,
121
+ vars: Record<string, any>,
122
+ ): Promise<void> {
123
+ if (!this.transformFn || !jsonResp.data) return;
124
+
125
+ const transformed = await this.transformFn(jsonResp.data, vars);
126
+ if (typeof transformed !== 'undefined') jsonResp.data = transformed;
127
+ }
128
+
99
129
  async handle(
100
130
  caching: QueriesCaching,
101
131
  csr: CrelteServerRequest,
@@ -187,6 +217,7 @@ export class QueryRoute {
187
217
  jsonResp = await resp.json();
188
218
  if (!jsonResp || typeof jsonResp !== 'object')
189
219
  throw new Error('invalid json response');
220
+ await this.transform(jsonResp, vars);
190
221
  } catch (e) {
191
222
  return newError(e, 500);
192
223
  }