@aeriajs/core 0.0.169 → 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.
@@ -221,6 +221,7 @@ const moveFiles = async (value, ctx) => {
221
221
  }
222
222
  const { _id: fileId, ...newFile } = tempFile;
223
223
  newFile.absolute_path = `${ctx.options.context.config.storage.fs}/${tempFile.absolute_path.split(path.sep).at(-1)}`;
224
+ newFile.owner = ctx.options.context.token.sub;
224
225
  await fs.rename(tempFile.absolute_path, newFile.absolute_path);
225
226
  const file = await ctx.options.context.collections.file.model.insertOne(newFile);
226
227
  return file.insertedId;
@@ -186,6 +186,7 @@ const moveFiles = async (value, ctx) => {
186
186
  }
187
187
  const { _id: fileId, ...newFile } = tempFile;
188
188
  newFile.absolute_path = `${ctx.options.context.config.storage.fs}/${tempFile.absolute_path.split(path.sep).at(-1)}`;
189
+ newFile.owner = ctx.options.context.token.sub;
189
190
  await fs.rename(tempFile.absolute_path, newFile.absolute_path);
190
191
  const file = await ctx.options.context.collections.file.model.insertOne(newFile);
191
192
  return file.insertedId;
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,45 +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 = {}, } = options;
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 || {
69
+ authenticated: false,
70
+ sub: null,
71
+ }, } = options;
65
72
  const { getCollectionAsset } = await Promise.resolve().then(() => __importStar(require('./assets.js')));
66
73
  const collections = await (0, entrypoint_1.getCollections)();
67
- const context = Object.assign({}, parentContext);
68
- Object.assign(context, options);
69
- context.log = async (message, details) => {
70
- return (0, database_js_1.getDatabaseCollection)('log').insertOne({
71
- message,
72
- details,
73
- context: collectionName,
74
- owner: token.authenticated
75
- ? token.sub
76
- : options.parentContext?.token.sub,
77
- created_at: new Date(),
78
- });
79
- };
80
- context.error = (httpStatus, error) => {
81
- return (0, common_1.endpointError)(Object.assign({
82
- httpStatus,
83
- }, error));
84
- };
85
- context.limitRate = (params) => {
86
- return (0, security_1.limitRate)(params, context);
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 = {
97
+ token,
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
+ },
87
129
  };
88
- if (collectionName) {
130
+ if (isCollectionContext(context, collectionName) && collectionName) {
89
131
  const description = (0, common_1.throwIfError)(await getCollectionAsset(collectionName, 'description'));
90
132
  context.description = await (0, preload_js_1.preloadDescription)(description);
91
133
  context.collectionName = collectionName;
92
134
  context.collection = indepthCollection(collectionName, collections, context);
93
135
  }
94
- context.collections = new Proxy({}, {
95
- get: (_, collectionName) => {
96
- if (typeof collectionName !== 'string') {
97
- throw new Error();
98
- }
99
- return indepthCollection(collectionName, collections, context);
100
- },
101
- });
102
136
  return context;
103
137
  };
104
138
  exports.createContext = createContext;
package/dist/context.mjs CHANGED
@@ -33,46 +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
- parentContext = {},
40
- token = {}
43
+ parentContext,
44
+ token = parentContext?.token || {
45
+ authenticated: false,
46
+ sub: null
47
+ }
41
48
  } = options;
42
49
  const { getCollectionAsset } = await import("./assets.mjs");
43
50
  const collections = await getCollections();
44
- const context = Object.assign({}, parentContext);
45
- Object.assign(context, options);
46
- context.log = async (message, details) => {
47
- return getDatabaseCollection("log").insertOne({
48
- message,
49
- details,
50
- context: collectionName,
51
- owner: token.authenticated ? token.sub : options.parentContext?.token.sub,
52
- created_at: /* @__PURE__ */ new Date()
53
- });
54
- };
55
- context.error = (httpStatus, error) => {
56
- return endpointError(Object.assign({
57
- httpStatus
58
- }, error));
59
- };
60
- context.limitRate = (params) => {
61
- 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
+ }
62
101
  };
63
- if (collectionName) {
102
+ if (isCollectionContext(context, collectionName) && collectionName) {
64
103
  const description = throwIfError(await getCollectionAsset(collectionName, "description"));
65
104
  context.description = await preloadDescription(description);
66
105
  context.collectionName = collectionName;
67
106
  context.collection = indepthCollection(collectionName, collections, context);
68
107
  }
69
- context.collections = new Proxy({}, {
70
- get: (_, collectionName2) => {
71
- if (typeof collectionName2 !== "string") {
72
- throw new Error();
73
- }
74
- return indepthCollection(collectionName2, collections, context);
75
- }
76
- });
77
108
  return context;
78
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.169",
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.169",
46
- "@aeriajs/common": "^0.0.104",
47
- "@aeriajs/entrypoint": "^0.0.106",
48
- "@aeriajs/http": "^0.0.117",
49
- "@aeriajs/security": "^0.0.169",
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",