@aeriajs/security 0.0.187 → 0.0.189

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.
@@ -34,8 +34,12 @@ export declare const getOrCreateUsageEntry: (params: RateLimitingParams, context
34
34
  };
35
35
  };
36
36
  };
37
- }>>, "_id">>>;
37
+ }>>, "_id">> | null>;
38
38
  export declare const limitRate: (params: RateLimitingParams, context: RouteContext) => Promise<Result.Error<{
39
+ readonly code: RateLimitingError.Unauthenticated;
40
+ } & {
41
+ httpStatus: HTTPStatus.InternalServerError;
42
+ }> | Result.Error<{
39
43
  readonly code: RateLimitingError.LimitReached;
40
44
  } & {
41
45
  httpStatus: HTTPStatus.TooManyRequests;
@@ -9,7 +9,7 @@ const buildEntryFilter = (params, context) => {
9
9
  address,
10
10
  };
11
11
  }
12
- if (!context.token.authenticated) {
12
+ if (!context.token.sub) {
13
13
  throw new Error('user is not authenticated');
14
14
  }
15
15
  return {
@@ -18,7 +18,7 @@ const buildEntryFilter = (params, context) => {
18
18
  };
19
19
  const getOrCreateUsageEntry = async (params, context) => {
20
20
  const filters = buildEntryFilter(params, context);
21
- const entry = await context.collections.resourceUsage.model.findOneAndUpdate(filters, {
21
+ return context.collections.resourceUsage.model.findOneAndUpdate(filters, {
22
22
  $setOnInsert: {
23
23
  usage: {},
24
24
  },
@@ -26,15 +26,16 @@ const getOrCreateUsageEntry = async (params, context) => {
26
26
  upsert: true,
27
27
  returnDocument: 'after',
28
28
  });
29
- if (!entry) {
30
- throw new Error('there was an error creating the entry');
31
- }
32
- return entry;
33
29
  };
34
30
  exports.getOrCreateUsageEntry = getOrCreateUsageEntry;
35
31
  const limitRate = async (params, context) => {
36
32
  const { increment = 1 } = params;
37
33
  const entry = await (0, exports.getOrCreateUsageEntry)(params, context);
34
+ if (!entry) {
35
+ return context.error(types_1.HTTPStatus.InternalServerError, {
36
+ code: types_1.RateLimitingError.Unauthenticated,
37
+ });
38
+ }
38
39
  const pathname = context.request.url.replace(new RegExp(`^${context.config.baseUrl}`), '');
39
40
  const resourceName = new URL(`http://0.com${pathname}`).pathname;
40
41
  const resource = entry.usage[resourceName];
@@ -7,7 +7,7 @@ const buildEntryFilter = (params, context) => {
7
7
  address
8
8
  };
9
9
  }
10
- if (!context.token.authenticated) {
10
+ if (!context.token.sub) {
11
11
  throw new Error("user is not authenticated");
12
12
  }
13
13
  return {
@@ -16,7 +16,7 @@ const buildEntryFilter = (params, context) => {
16
16
  };
17
17
  export const getOrCreateUsageEntry = async (params, context) => {
18
18
  const filters = buildEntryFilter(params, context);
19
- const entry = await context.collections.resourceUsage.model.findOneAndUpdate(
19
+ return context.collections.resourceUsage.model.findOneAndUpdate(
20
20
  filters,
21
21
  {
22
22
  $setOnInsert: {
@@ -28,14 +28,15 @@ export const getOrCreateUsageEntry = async (params, context) => {
28
28
  returnDocument: "after"
29
29
  }
30
30
  );
31
- if (!entry) {
32
- throw new Error("there was an error creating the entry");
33
- }
34
- return entry;
35
31
  };
36
32
  export const limitRate = async (params, context) => {
37
33
  const { increment = 1 } = params;
38
34
  const entry = await getOrCreateUsageEntry(params, context);
35
+ if (!entry) {
36
+ return context.error(HTTPStatus.InternalServerError, {
37
+ code: RateLimitingError.Unauthenticated
38
+ });
39
+ }
39
40
  const pathname = context.request.url.replace(new RegExp(`^${context.config.baseUrl}`), "");
40
41
  const resourceName = new URL(`http://0.com${pathname}`).pathname;
41
42
  const resource = entry.usage[resourceName];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/security",
3
- "version": "0.0.187",
3
+ "version": "0.0.189",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -28,9 +28,9 @@
28
28
  "mongodb": "^6.5.0"
29
29
  },
30
30
  "peerDependencies": {
31
- "@aeriajs/core": "^0.0.187",
32
- "@aeriajs/common": "^0.0.114",
33
- "@aeriajs/types": "^0.0.97",
31
+ "@aeriajs/core": "^0.0.189",
32
+ "@aeriajs/common": "^0.0.115",
33
+ "@aeriajs/types": "^0.0.98",
34
34
  "mongodb": "^6.5.0"
35
35
  },
36
36
  "scripts": {