@aeriajs/core 0.0.170 → 0.0.171

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/dist/context.d.ts CHANGED
@@ -1,2 +1,4 @@
1
- import type { Context, ContextOptions } from '@aeriajs/types';
2
- export declare const createContext: (options?: ContextOptions) => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, Record<string, (payload: any, context: Context<any>, ...args: any[]) => any> | undefined>>;
1
+ import type { Context, ContextOptions, RouteContext } from '@aeriajs/types';
2
+ export declare const createContext: <TContextOptions extends ContextOptions>(_options?: TContextOptions) => Promise<TContextOptions extends {
3
+ collectionName: unknown;
4
+ } ? Context : RouteContext>;
package/dist/context.js CHANGED
@@ -60,51 +60,79 @@ const indepthCollection = (collectionName, collections, parentContext) => {
60
60
  model: (0, database_js_1.getDatabaseCollection)(collectionName),
61
61
  };
62
62
  };
63
- const createContext = async (options = {}) => {
64
- const { collectionName, parentContext, token = {
63
+ const isCollectionContext = (_context, collectionName) => {
64
+ return !!collectionName;
65
+ };
66
+ const createContext = async (_options) => {
67
+ const options = _options || {};
68
+ const { collectionName, parentContext, token = parentContext?.token || {
65
69
  authenticated: false,
66
70
  sub: null,
67
71
  }, } = options;
68
72
  const { getCollectionAsset } = await Promise.resolve().then(() => __importStar(require('./assets.js')));
69
73
  const collections = await (0, entrypoint_1.getCollections)();
70
- const context = Object.assign({}, parentContext);
71
- Object.assign(context, {
72
- ...options,
74
+ let config;
75
+ if (options.config) {
76
+ config = options.config;
77
+ }
78
+ else if (parentContext?.config) {
79
+ config = parentContext.config;
80
+ }
81
+ else {
82
+ config = {
83
+ security: {},
84
+ };
85
+ }
86
+ let request, response, inherited = !!options.inherited;
87
+ if (parentContext) {
88
+ request = parentContext.request;
89
+ response = parentContext.response;
90
+ inherited ||= parentContext.inherited;
91
+ }
92
+ else {
93
+ request = {};
94
+ response = {};
95
+ }
96
+ const context = {
73
97
  token,
74
- });
75
- context.log = async (message, details) => {
76
- return (0, database_js_1.getDatabaseCollection)('log').insertOne({
77
- message,
78
- details,
79
- context: collectionName,
80
- owner: token.authenticated
81
- ? token.sub
82
- : options.parentContext?.token.sub,
83
- created_at: new Date(),
84
- });
85
- };
86
- context.error = (httpStatus, error) => {
87
- return (0, common_1.endpointError)(Object.assign({
88
- httpStatus,
89
- }, error));
90
- };
91
- context.limitRate = (params) => {
92
- return (0, security_1.limitRate)(params, context);
98
+ config,
99
+ inherited,
100
+ request,
101
+ response,
102
+ collections: new Proxy({}, {
103
+ get: (_, collectionName) => {
104
+ if (typeof collectionName !== 'string') {
105
+ throw new Error();
106
+ }
107
+ return indepthCollection(collectionName, collections, context);
108
+ },
109
+ }),
110
+ log: async (message, details) => {
111
+ return (0, database_js_1.getDatabaseCollection)('log').insertOne({
112
+ message,
113
+ details,
114
+ context: collectionName,
115
+ owner: token.authenticated
116
+ ? token.sub
117
+ : options.parentContext?.token.sub,
118
+ created_at: new Date(),
119
+ });
120
+ },
121
+ error: (httpStatus, error) => {
122
+ return (0, common_1.endpointError)(Object.assign({
123
+ httpStatus,
124
+ }, error));
125
+ },
126
+ limitRate: (params) => {
127
+ return (0, security_1.limitRate)(params, context);
128
+ },
93
129
  };
94
- if (collectionName) {
130
+ if (isCollectionContext(context, collectionName) && collectionName) {
95
131
  const description = (0, common_1.throwIfError)(await getCollectionAsset(collectionName, 'description'));
96
132
  context.description = await (0, preload_js_1.preloadDescription)(description);
97
133
  context.collectionName = collectionName;
98
134
  context.collection = indepthCollection(collectionName, collections, context);
99
135
  }
100
- context.collections = new Proxy({}, {
101
- get: (_, collectionName) => {
102
- if (typeof collectionName !== 'string') {
103
- throw new Error();
104
- }
105
- return indepthCollection(collectionName, collections, context);
106
- },
107
- });
108
136
  return context;
109
137
  };
110
138
  exports.createContext = createContext;
package/dist/context.mjs CHANGED
@@ -33,52 +33,77 @@ const indepthCollection = (collectionName, collections, parentContext) => {
33
33
  model: getDatabaseCollection(collectionName)
34
34
  };
35
35
  };
36
- export const createContext = async (options = {}) => {
36
+ const isCollectionContext = (_context, collectionName) => {
37
+ return !!collectionName;
38
+ };
39
+ export const createContext = async (_options) => {
40
+ const options = _options || {};
37
41
  const {
38
42
  collectionName,
39
43
  parentContext,
40
- token = {
44
+ token = parentContext?.token || {
41
45
  authenticated: false,
42
46
  sub: null
43
47
  }
44
48
  } = options;
45
49
  const { getCollectionAsset } = await import("./assets.mjs");
46
50
  const collections = await getCollections();
47
- const context = Object.assign({}, parentContext);
48
- Object.assign(context, {
49
- ...options,
50
- token
51
- });
52
- context.log = async (message, details) => {
53
- return getDatabaseCollection("log").insertOne({
54
- message,
55
- details,
56
- context: collectionName,
57
- owner: token.authenticated ? token.sub : options.parentContext?.token.sub,
58
- created_at: /* @__PURE__ */ new Date()
59
- });
60
- };
61
- context.error = (httpStatus, error) => {
62
- return endpointError(Object.assign({
63
- httpStatus
64
- }, error));
65
- };
66
- context.limitRate = (params) => {
67
- return limitRate(params, context);
51
+ let config;
52
+ if (options.config) {
53
+ config = options.config;
54
+ } else if (parentContext?.config) {
55
+ config = parentContext.config;
56
+ } else {
57
+ config = {
58
+ security: {}
59
+ };
60
+ }
61
+ let request, response, inherited = !!options.inherited;
62
+ if (parentContext) {
63
+ request = parentContext.request;
64
+ response = parentContext.response;
65
+ inherited ||= parentContext.inherited;
66
+ } else {
67
+ request = {};
68
+ response = {};
69
+ }
70
+ const context = {
71
+ token,
72
+ config,
73
+ inherited,
74
+ request,
75
+ response,
76
+ collections: new Proxy({}, {
77
+ get: (_, collectionName2) => {
78
+ if (typeof collectionName2 !== "string") {
79
+ throw new Error();
80
+ }
81
+ return indepthCollection(collectionName2, collections, context);
82
+ }
83
+ }),
84
+ log: async (message, details) => {
85
+ return getDatabaseCollection("log").insertOne({
86
+ message,
87
+ details,
88
+ context: collectionName,
89
+ owner: token.authenticated ? token.sub : options.parentContext?.token.sub,
90
+ created_at: /* @__PURE__ */ new Date()
91
+ });
92
+ },
93
+ error: (httpStatus, error) => {
94
+ return endpointError(Object.assign({
95
+ httpStatus
96
+ }, error));
97
+ },
98
+ limitRate: (params) => {
99
+ return limitRate(params, context);
100
+ }
68
101
  };
69
- if (collectionName) {
102
+ if (isCollectionContext(context, collectionName) && collectionName) {
70
103
  const description = throwIfError(await getCollectionAsset(collectionName, "description"));
71
104
  context.description = await preloadDescription(description);
72
105
  context.collectionName = collectionName;
73
106
  context.collection = indepthCollection(collectionName, collections, context);
74
107
  }
75
- context.collections = new Proxy({}, {
76
- get: (_, collectionName2) => {
77
- if (typeof collectionName2 !== "string") {
78
- throw new Error();
79
- }
80
- return indepthCollection(collectionName2, collections, context);
81
- }
82
- });
83
108
  return context;
84
109
  };
package/dist/use.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext<null> & import("@aeriajs/types").CollectionContext<import("@aeriajs/types").Description, Record<string, (payload: any, context: import("@aeriajs/types").Context<any>, ...args: any[]) => any> | undefined>>;
1
+ export declare const createAeria: () => Promise<import("@aeriajs/types").RouteContext>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/core",
3
- "version": "0.0.170",
3
+ "version": "0.0.171",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "aeriaMain": "tests/fixtures/aeriaMain.js",
@@ -42,13 +42,13 @@
42
42
  "mongodb-memory-server": "^9.2.0"
43
43
  },
44
44
  "peerDependencies": {
45
- "@aeriajs/builtins": "^0.0.170",
46
- "@aeriajs/common": "^0.0.104",
47
- "@aeriajs/entrypoint": "^0.0.106",
48
- "@aeriajs/http": "^0.0.117",
49
- "@aeriajs/security": "^0.0.170",
50
- "@aeriajs/types": "^0.0.87",
51
- "@aeriajs/validation": "^0.0.107"
45
+ "@aeriajs/builtins": "^0.0.171",
46
+ "@aeriajs/common": "^0.0.105",
47
+ "@aeriajs/entrypoint": "^0.0.107",
48
+ "@aeriajs/http": "^0.0.118",
49
+ "@aeriajs/security": "^0.0.171",
50
+ "@aeriajs/types": "^0.0.88",
51
+ "@aeriajs/validation": "^0.0.108"
52
52
  },
53
53
  "dependencies": {
54
54
  "mongodb": "^6.5.0",