@tmlmobilidade/utils 20251031.1051.3 → 20251202.1817.5
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/README.md +239 -9
- package/dist/{src/http.js → http.js} +1 -1
- package/dist/index.d.ts +8 -17
- package/dist/index.js +8 -17
- package/dist/objects/fill-template.d.ts +7 -0
- package/dist/objects/fill-template.js +52 -0
- package/dist/{src/objects → objects}/index.d.ts +1 -0
- package/dist/{src/objects → objects}/index.js +1 -0
- package/dist/permissions.d.ts +46 -0
- package/dist/permissions.js +86 -0
- package/dist/{src/validate-query-params.js → validate-query-params.js} +1 -1
- package/package.json +21 -35
- package/dist/src/css/get-variable-value.d.ts +0 -1
- package/dist/src/css/get-variable-value.js +0 -8
- package/dist/src/css/index.d.ts +0 -1
- package/dist/src/css/index.js +0 -1
- package/dist/src/dates/dates.d.ts +0 -175
- package/dist/src/dates/dates.js +0 -385
- package/dist/src/dates/format.d.ts +0 -27
- package/dist/src/dates/format.js +0 -99
- package/dist/src/dates/index.d.ts +0 -4
- package/dist/src/dates/index.js +0 -4
- package/dist/src/dates/types.d.ts +0 -4
- package/dist/src/dates/types.js +0 -338
- package/dist/src/dates/utils.d.ts +0 -17
- package/dist/src/dates/utils.js +0 -53
- package/dist/src/files/files.d.ts +0 -64
- package/dist/src/files/files.js +0 -152
- package/dist/src/files/utils.d.ts +0 -20
- package/dist/src/files/utils.js +0 -47
- package/dist/src/geo/chunk-line.d.ts +0 -10
- package/dist/src/geo/chunk-line.js +0 -75
- package/dist/src/geo/constants.d.ts +0 -8
- package/dist/src/geo/constants.js +0 -8
- package/dist/src/geo/conversions.d.ts +0 -36
- package/dist/src/geo/conversions.js +0 -79
- package/dist/src/geo/coordinates.d.ts +0 -37
- package/dist/src/geo/coordinates.js +0 -64
- package/dist/src/geo/cut-line-at-length.d.ts +0 -10
- package/dist/src/geo/cut-line-at-length.js +0 -68
- package/dist/src/geo/geojson-collections.d.ts +0 -11
- package/dist/src/geo/geojson-collections.js +0 -29
- package/dist/src/geo/get-geofence-on-point.d.ts +0 -17
- package/dist/src/geo/get-geofence-on-point.js +0 -46
- package/dist/src/geo/index.d.ts +0 -9
- package/dist/src/geo/index.js +0 -9
- package/dist/src/geo/is-point-in-polygon.d.ts +0 -9
- package/dist/src/geo/is-point-in-polygon.js +0 -37
- package/dist/src/geo/measurements.d.ts +0 -42
- package/dist/src/geo/measurements.js +0 -71
- package/dist/src/logs/index.d.ts +0 -1
- package/dist/src/logs/index.js +0 -1
- package/dist/src/logs/logs.d.ts +0 -82
- package/dist/src/logs/logs.js +0 -145
- package/dist/src/math/coefficient-of-variation.d.ts +0 -6
- package/dist/src/math/coefficient-of-variation.js +0 -14
- package/dist/src/math/entropy.d.ts +0 -6
- package/dist/src/math/entropy.js +0 -20
- package/dist/src/math/index.d.ts +0 -3
- package/dist/src/math/index.js +0 -3
- package/dist/src/math/round-number-bias.d.ts +0 -6
- package/dist/src/math/round-number-bias.js +0 -11
- package/dist/src/navigation/index.d.ts +0 -1
- package/dist/src/navigation/index.js +0 -1
- package/dist/src/navigation/keep-url-params.d.ts +0 -8
- package/dist/src/navigation/keep-url-params.js +0 -13
- package/dist/src/nuqs/index.d.ts +0 -1
- package/dist/src/nuqs/index.js +0 -2
- package/dist/src/nuqs/parse-as-array-of-strings.d.ts +0 -1
- package/dist/src/nuqs/parse-as-array-of-strings.js +0 -43
- package/dist/src/permissions.d.ts +0 -46
- package/dist/src/permissions.js +0 -77
- package/dist/src/random/generate-random-number.d.ts +0 -8
- package/dist/src/random/generate-random-number.js +0 -14
- package/dist/src/random/generate-random-string.d.ts +0 -12
- package/dist/src/random/generate-random-string.js +0 -32
- package/dist/src/random/generate-random-token.d.ts +0 -8
- package/dist/src/random/generate-random-token.js +0 -12
- package/dist/src/random/index.d.ts +0 -3
- package/dist/src/random/index.js +0 -3
- package/dist/src/strings/index.d.ts +0 -2
- package/dist/src/strings/index.js +0 -2
- package/dist/src/strings/normalize-string.d.ts +0 -8
- package/dist/src/strings/normalize-string.js +0 -18
- package/dist/src/strings/validation.d.ts +0 -2
- package/dist/src/strings/validation.js +0 -7
- /package/dist/{src/batching → batching}/index.d.ts +0 -0
- /package/dist/{src/batching → batching}/index.js +0 -0
- /package/dist/{src/batching → batching}/perform-in-chunks.d.ts +0 -0
- /package/dist/{src/batching → batching}/perform-in-chunks.js +0 -0
- /package/dist/{src/caching → caching}/cache.d.ts +0 -0
- /package/dist/{src/caching → caching}/cache.js +0 -0
- /package/dist/{src/caching → caching}/index.d.ts +0 -0
- /package/dist/{src/caching → caching}/index.js +0 -0
- /package/dist/{src/generic → generic}/get-value-at-path.d.ts +0 -0
- /package/dist/{src/generic → generic}/get-value-at-path.js +0 -0
- /package/dist/{src/generic → generic}/index.d.ts +0 -0
- /package/dist/{src/generic → generic}/index.js +0 -0
- /package/dist/{src/generic → generic}/set-value-at-path.d.ts +0 -0
- /package/dist/{src/generic → generic}/set-value-at-path.js +0 -0
- /package/dist/{src/http.d.ts → http.d.ts} +0 -0
- /package/dist/{src/objects → objects}/compare-objects.d.ts +0 -0
- /package/dist/{src/objects → objects}/compare-objects.js +0 -0
- /package/dist/{src/objects → objects}/convert-object.d.ts +0 -0
- /package/dist/{src/objects → objects}/convert-object.js +0 -0
- /package/dist/{src/objects → objects}/flatten-object.d.ts +0 -0
- /package/dist/{src/objects → objects}/flatten-object.js +0 -0
- /package/dist/{src/objects → objects}/merge-objects.d.ts +0 -0
- /package/dist/{src/objects → objects}/merge-objects.js +0 -0
- /package/dist/{src/singleton-proxy.d.ts → singleton-proxy.d.ts} +0 -0
- /package/dist/{src/singleton-proxy.js → singleton-proxy.js} +0 -0
- /package/dist/{src/validate-query-params.d.ts → validate-query-params.d.ts} +0 -0
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { type Permission } from '@tmlmobilidade/types';
|
|
2
|
-
/**
|
|
3
|
-
* Get a permission from a list of permissions
|
|
4
|
-
* @param permissions - The list of permissions
|
|
5
|
-
* @param scope - The scope of the permission
|
|
6
|
-
* @param action - The action of the permission
|
|
7
|
-
* @returns The permission
|
|
8
|
-
*/
|
|
9
|
-
export declare function getPermission(permissions: Permission<unknown>[], scope: string, action: string): Permission<unknown>;
|
|
10
|
-
/**
|
|
11
|
-
* Check if a permission exists in a list of permissions.
|
|
12
|
-
* @param permissions The list of permissions.
|
|
13
|
-
* @param scope The scope of the permission.
|
|
14
|
-
* @param action The action of the permission.
|
|
15
|
-
* @returns The permission object or undefined if not found.
|
|
16
|
-
*/
|
|
17
|
-
export declare function hasPermission(permissions: Permission<unknown>[], scope: string, action: string): boolean;
|
|
18
|
-
export interface HasPermissionResourceArgs<T> {
|
|
19
|
-
action: string;
|
|
20
|
-
permissions?: Permission<T>[];
|
|
21
|
-
resource_key: keyof T;
|
|
22
|
-
scope: string;
|
|
23
|
-
value: unknown;
|
|
24
|
-
}
|
|
25
|
-
type HasPermissionResourceArgsWithPermissions<T> = HasPermissionResourceArgs<T> & {
|
|
26
|
-
permissions: Permission<T>[];
|
|
27
|
-
};
|
|
28
|
-
/**
|
|
29
|
-
* Check if a value exists in a resource of a permission
|
|
30
|
-
* @param permissions - The list of permissions
|
|
31
|
-
* @param value - The value to check
|
|
32
|
-
* @param resource_key - The key of the resource
|
|
33
|
-
* @param scope - The scope of the permission
|
|
34
|
-
* @param action - The action of the permission
|
|
35
|
-
* @returns The permission
|
|
36
|
-
*/
|
|
37
|
-
export declare function hasPermissionResource<T>({ action, permissions, resource_key, scope, value }: HasPermissionResourceArgsWithPermissions<T>): boolean;
|
|
38
|
-
/**
|
|
39
|
-
* Ensure a permission is granted in a fastify request
|
|
40
|
-
* @param request - The FastifyRequest
|
|
41
|
-
* @param reply - The FastifyReply
|
|
42
|
-
* @param params - The parameters of the permission
|
|
43
|
-
* @returns The allowed
|
|
44
|
-
*/
|
|
45
|
-
export declare function hasAPIResourcePermission<T>(request: any, params: HasPermissionResourceArgs<T>): boolean;
|
|
46
|
-
export {};
|
package/dist/src/permissions.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/* * */
|
|
3
|
-
import { ALLOW_ALL_FLAG } from '@tmlmobilidade/lib';
|
|
4
|
-
import { mergekit } from 'mergekit';
|
|
5
|
-
/**
|
|
6
|
-
* Get a permission from a list of permissions
|
|
7
|
-
* @param permissions - The list of permissions
|
|
8
|
-
* @param scope - The scope of the permission
|
|
9
|
-
* @param action - The action of the permission
|
|
10
|
-
* @returns The permission
|
|
11
|
-
*/
|
|
12
|
-
export function getPermission(permissions, scope, action) {
|
|
13
|
-
return mergekit([...(permissions ?? [])], {
|
|
14
|
-
appendArrays: true,
|
|
15
|
-
dedupArrays: true,
|
|
16
|
-
onlyObjectWithKeyValues: [
|
|
17
|
-
{ key: 'scope', value: scope },
|
|
18
|
-
{ key: 'action', value: action },
|
|
19
|
-
],
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Check if a permission exists in a list of permissions.
|
|
24
|
-
* @param permissions The list of permissions.
|
|
25
|
-
* @param scope The scope of the permission.
|
|
26
|
-
* @param action The action of the permission.
|
|
27
|
-
* @returns The permission object or undefined if not found.
|
|
28
|
-
*/
|
|
29
|
-
export function hasPermission(permissions, scope, action) {
|
|
30
|
-
return permissions.find(permission => permission.scope === scope && permission.action === action) !== undefined;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Check if a value exists in a resource of a permission
|
|
34
|
-
* @param permissions - The list of permissions
|
|
35
|
-
* @param value - The value to check
|
|
36
|
-
* @param resource_key - The key of the resource
|
|
37
|
-
* @param scope - The scope of the permission
|
|
38
|
-
* @param action - The action of the permission
|
|
39
|
-
* @returns The permission
|
|
40
|
-
*/
|
|
41
|
-
export function hasPermissionResource({ action, permissions, resource_key, scope, value }) {
|
|
42
|
-
if (!permissions)
|
|
43
|
-
return false;
|
|
44
|
-
const permission = permissions.find(permission => permission.action === action && permission.scope === scope);
|
|
45
|
-
if (!permission)
|
|
46
|
-
return false;
|
|
47
|
-
// Check if value exists in the permission.resource[resource_key]
|
|
48
|
-
const resourceValues = permission.resource?.[resource_key];
|
|
49
|
-
if (!resourceValues)
|
|
50
|
-
return false;
|
|
51
|
-
// If is Array
|
|
52
|
-
if (Array.isArray(resourceValues) && resourceValues.includes(ALLOW_ALL_FLAG))
|
|
53
|
-
return true;
|
|
54
|
-
if (Array.isArray(resourceValues) && resourceValues.includes(value))
|
|
55
|
-
return true;
|
|
56
|
-
// If is not Array, check if value is equal to resourceValues
|
|
57
|
-
if (resourceValues === value)
|
|
58
|
-
return true;
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Ensure a permission is granted in a fastify request
|
|
63
|
-
* @param request - The FastifyRequest
|
|
64
|
-
* @param reply - The FastifyReply
|
|
65
|
-
* @param params - The parameters of the permission
|
|
66
|
-
* @returns The allowed
|
|
67
|
-
*/
|
|
68
|
-
export function hasAPIResourcePermission(request, params) {
|
|
69
|
-
const allowed = hasPermissionResource({
|
|
70
|
-
action: params.action,
|
|
71
|
-
permissions: request.permissions,
|
|
72
|
-
resource_key: params.resource_key,
|
|
73
|
-
scope: params.scope,
|
|
74
|
-
value: params.value,
|
|
75
|
-
});
|
|
76
|
-
return allowed;
|
|
77
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a secure random number between 0 and 1.
|
|
3
|
-
* This function uses the Web Crypto API so it is suitable
|
|
4
|
-
* for cryptographic purposes.
|
|
5
|
-
* @returns A random number between 0 and 1
|
|
6
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
|
7
|
-
*/
|
|
8
|
-
export declare function generateRandomNumber(): number;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/* * */
|
|
2
|
-
import crypto from 'crypto';
|
|
3
|
-
/**
|
|
4
|
-
* Generates a secure random number between 0 and 1.
|
|
5
|
-
* This function uses the Web Crypto API so it is suitable
|
|
6
|
-
* for cryptographic purposes.
|
|
7
|
-
* @returns A random number between 0 and 1
|
|
8
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
|
9
|
-
*/
|
|
10
|
-
export function generateRandomNumber() {
|
|
11
|
-
const array = new Uint32Array(1);
|
|
12
|
-
crypto.getRandomValues(array);
|
|
13
|
-
return array[0] / 2 ** 32;
|
|
14
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
interface GenerateRandomStringProps {
|
|
2
|
-
length?: number;
|
|
3
|
-
type?: 'alphabetic' | 'alphanumeric' | 'numeric';
|
|
4
|
-
}
|
|
5
|
-
/**
|
|
6
|
-
* Creates a random string of a given length and type.
|
|
7
|
-
* @param length The length of the string to generate. Defaults to `6`.
|
|
8
|
-
* @param type The type of characters to include in the string. Defaults to `alphanumeric`.
|
|
9
|
-
* @returns A random string of the specified length.
|
|
10
|
-
*/
|
|
11
|
-
export declare function generateRandomString({ length, type }?: GenerateRandomStringProps): string;
|
|
12
|
-
export {};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/* * */
|
|
2
|
-
import { generateRandomNumber } from './generate-random-number.js';
|
|
3
|
-
/**
|
|
4
|
-
* Creates a random string of a given length and type.
|
|
5
|
-
* @param length The length of the string to generate. Defaults to `6`.
|
|
6
|
-
* @param type The type of characters to include in the string. Defaults to `alphanumeric`.
|
|
7
|
-
* @returns A random string of the specified length.
|
|
8
|
-
*/
|
|
9
|
-
export function generateRandomString({ length = 6, type = 'alphanumeric' } = {}) {
|
|
10
|
-
//
|
|
11
|
-
const numericSet = '0123456789';
|
|
12
|
-
const alphabeticSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
13
|
-
const alphanumericSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
14
|
-
let allowedCharacters;
|
|
15
|
-
switch (type) {
|
|
16
|
-
case 'alphabetic':
|
|
17
|
-
allowedCharacters = alphabeticSet;
|
|
18
|
-
break;
|
|
19
|
-
case 'numeric':
|
|
20
|
-
allowedCharacters = numericSet;
|
|
21
|
-
break;
|
|
22
|
-
default:
|
|
23
|
-
allowedCharacters = alphanumericSet;
|
|
24
|
-
break;
|
|
25
|
-
}
|
|
26
|
-
let result = '';
|
|
27
|
-
for (let i = 0; i < length; i++) {
|
|
28
|
-
result += allowedCharacters.charAt(Math.floor(generateRandomNumber() * allowedCharacters.length));
|
|
29
|
-
}
|
|
30
|
-
return result;
|
|
31
|
-
//
|
|
32
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generates a secure random token.
|
|
3
|
-
* This function uses the Node.js crypto module to generate
|
|
4
|
-
* a random token suitable for cryptographic purposes.
|
|
5
|
-
* @returns A random token as a hexadecimal string.
|
|
6
|
-
* @see https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
|
|
7
|
-
*/
|
|
8
|
-
export declare function generateRandomToken(): string;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/* * */
|
|
2
|
-
import crypto from 'crypto';
|
|
3
|
-
/**
|
|
4
|
-
* Generates a secure random token.
|
|
5
|
-
* This function uses the Node.js crypto module to generate
|
|
6
|
-
* a random token suitable for cryptographic purposes.
|
|
7
|
-
* @returns A random token as a hexadecimal string.
|
|
8
|
-
* @see https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
|
|
9
|
-
*/
|
|
10
|
-
export function generateRandomToken() {
|
|
11
|
-
return crypto.randomBytes(32).toString('hex');
|
|
12
|
-
}
|
package/dist/src/random/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Normalizes a string by converting it to lowercase, removing diacritics,
|
|
3
|
-
* and replacing accented characters with their non-accented equivalents.
|
|
4
|
-
* This is useful for case-insensitive and accent-insensitive string comparisons.
|
|
5
|
-
* @param value The string to normalize.
|
|
6
|
-
* @returns The normalized string.
|
|
7
|
-
*/
|
|
8
|
-
export declare function normalizeString(value: string): string;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/* * */
|
|
2
|
-
/**
|
|
3
|
-
* Normalizes a string by converting it to lowercase, removing diacritics,
|
|
4
|
-
* and replacing accented characters with their non-accented equivalents.
|
|
5
|
-
* This is useful for case-insensitive and accent-insensitive string comparisons.
|
|
6
|
-
* @param value The string to normalize.
|
|
7
|
-
* @returns The normalized string.
|
|
8
|
-
*/
|
|
9
|
-
export function normalizeString(value) {
|
|
10
|
-
// Return an empty string if the value is invalid
|
|
11
|
-
if (typeof value !== 'string')
|
|
12
|
-
return '';
|
|
13
|
-
// Normalize the string
|
|
14
|
-
return value
|
|
15
|
-
.toLowerCase()
|
|
16
|
-
.normalize('NFD')
|
|
17
|
-
.replace(/[\u0300-\u036f]/g, '');
|
|
18
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|