rajt 0.0.58 → 0.0.59

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/src/auth/auth.ts DELETED
@@ -1,33 +0,0 @@
1
- import { Authnz } from './authnz'
2
- import { Token } from './token'
3
-
4
- export class Auth {
5
- static #u: Authnz<any> | null = null
6
-
7
- static resolve() {
8
- this.#u = Authnz.fromToken(Token.fromRequest())
9
- }
10
-
11
- static get user() {
12
- return this.#u ? this.#u?.data : null
13
- }
14
-
15
- static can(...abilities: string[]): boolean {
16
- return this.#u ? this.#u.can(...abilities) : false
17
- }
18
-
19
- static cant(...abilities: string[]): boolean {
20
- return !this.can(...abilities)
21
- }
22
-
23
- static hasRole(...roles: string[]): boolean {
24
- return this.#u ? this.#u.hasRole(...roles) : false
25
- }
26
-
27
- static has(prop: string, value: any = null): boolean {
28
- return this.#u ? this.#u.has(prop, value) : false
29
- }
30
- static hasValue(prop: string, value: any = null): boolean {
31
- return this.has(prop, value)
32
- }
33
- }
@@ -1,125 +0,0 @@
1
- import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
2
- import {
3
- DynamoDBDocumentClient,
4
-
5
- BatchGetCommand,
6
- BatchWriteCommand,
7
-
8
- DeleteCommand,
9
- GetCommand,
10
- PutCommand,
11
- QueryCommand,
12
- ScanCommand,
13
- UpdateCommand,
14
-
15
- ScanCommandInput,
16
- QueryCommandInput,
17
- UpdateCommandInput,
18
-
19
- BatchGetCommandInput,
20
- BatchWriteCommandInput,
21
- } from '@aws-sdk/lib-dynamodb'
22
- import type { NativeAttributeValue } from '@aws-sdk/util-dynamodb'
23
- import AbstractModel from './model'
24
- import { Keys, KeySchema } from './types'
25
-
26
- const client = new DynamoDBClient(process.env?.AWS_SAM_LOCAL ? {
27
- region: process.env.AWS_REGION || 'us-east-1',
28
- endpoint: process.env.AWS_ENDPOINT_URL || undefined,
29
- credentials: {
30
- accessKeyId: process.env.AWS_ACCESS_KEY_ID || 'DUMMYIDEXAMPLE',
31
- secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || 'DUMMYEXAMPLEKEY',
32
- },
33
- } : {})
34
-
35
- export const DocumentClient = DynamoDBDocumentClient.from(client)
36
-
37
- export class Dynamodb {
38
- static model<T extends object>(cls: new (...args: any[]) => T) {
39
- return new AbstractModel<T>(cls)
40
- }
41
-
42
- static raw() {
43
- return RawClient
44
- }
45
- }
46
-
47
- export class RawClient {
48
- static async get(TableName: string, key: Keys | Record<string, string>, sk?: string) {
49
- return DocumentClient.send(new GetCommand({ TableName, Key: this.key(key, sk) }))
50
- }
51
-
52
- static async scan(TableName: string, filters: Omit<ScanCommandInput, 'TableName'>) {
53
- return DocumentClient.send(new ScanCommand({ ...filters, TableName }))
54
- }
55
-
56
- static async query(TableName: string, filters: Omit<QueryCommandInput, 'TableName'>) {
57
- return DocumentClient.send(new QueryCommand({ ...filters, TableName }))
58
- }
59
-
60
- static async put(TableName: string, Item: Record<string, NativeAttributeValue>) {
61
- return DocumentClient.send(new PutCommand({ TableName, Item }))
62
- }
63
-
64
- static async update(
65
- TableName: string,
66
- filters: Omit<UpdateCommandInput, 'TableName' | 'Key'>,
67
- key: Keys | Record<string, string>,
68
- sk?: string
69
- ) {
70
- return DocumentClient.send(new UpdateCommand({
71
- ...filters, TableName, Key: this.key(key, sk),
72
- }))
73
- }
74
-
75
- static async delete(TableName: string, key: Keys | Record<string, string>, sk?: string) {
76
- return DocumentClient.send(new DeleteCommand({ TableName, Key: this.key(key, sk) }))
77
- }
78
-
79
- static async batchGet(batch: BatchGetCommandInput) {
80
- return DocumentClient.send(new BatchGetCommand(batch))
81
- }
82
-
83
- static async batchWrite(batch: BatchWriteCommandInput) {
84
- return DocumentClient.send(new BatchWriteCommand(batch))
85
- }
86
-
87
- static key(
88
- key: Keys | Record<string, string>,
89
- sk?: string,
90
- schema?: KeySchema,
91
- defaultSK?: string,
92
- ) {
93
- let pk: string
94
- let skValue: string | undefined
95
-
96
- if (Array.isArray(key)) {
97
- pk = key[0]
98
- skValue = key[1] ?? sk
99
- } else if (typeof key == 'object' && key != null) {
100
- return key
101
- } else {
102
- pk = key
103
- skValue = sk
104
- }
105
-
106
- if (!schema) {
107
- const keys = {PK: pk}
108
- // @ts-ignore
109
- if (skValue) keys.SK = skValue
110
- return keys
111
- }
112
-
113
- const keys = { [schema.PK]: pk }
114
-
115
- if (schema?.SK) {
116
- if (skValue) {
117
- keys[schema.SK] = skValue
118
- } else if (defaultSK) {
119
- keys[schema.SK] = defaultSK
120
- }
121
- }
122
-
123
- return keys
124
- }
125
- }
@@ -1,205 +0,0 @@
1
- import { getLength } from 't0n'
2
- import { isArraySchema } from './schema'
3
- import type { SchemaStructure } from './types'
4
-
5
- export default class Compact {
6
- static #typeRegex: RegExp
7
- static #reverseTypeRegex: RegExp
8
- static #reverseTypeMap: Record<string, string>
9
- static #typeMap: Record<string, string> = {
10
- // Boolean
11
- 'true': 'T',
12
- 'false': 'F',
13
- // Null
14
- 'null': 'N',
15
- // Array
16
- '[]': 'A',
17
- '["0"]': 'A0',
18
- '["1"]': 'A1',
19
- '["false"]': 'A2',
20
- '["true"]': 'A3',
21
- // Object
22
- '{}': 'O',
23
- // String
24
- '""': 'S',
25
- '"0"': 'S0',
26
- '"1"': 'S1',
27
- '"false"': 'S2',
28
- '"true"': 'S3',
29
- }
30
-
31
- static {
32
- const keys = []
33
- const values = []
34
- const reverseTypeMap: Record<string, string> = {}
35
- for (const key in this.#typeMap) {
36
- const val = this.#typeMap[key]
37
- const k = key.replace(/"/g, "'")
38
- keys.push(k)
39
- values.push(val)
40
-
41
- reverseTypeMap[val] = k
42
- this.#typeMap[k] = val
43
- }
44
-
45
- this.#reverseTypeMap = reverseTypeMap
46
- this.#typeRegex = this.#mapRegex(keys)
47
- this.#reverseTypeRegex = this.#mapRegex(values)
48
- }
49
-
50
- static encode(obj: any, schema: SchemaStructure): string {
51
- const seen: any[] = []
52
-
53
- return this.#pack(
54
- JSON.stringify(this.zip(obj, schema, seen))
55
- .replace(/"\^(\d+)"/g, '^$1')
56
- .replace(/"/g, '~TDQ~')
57
- .replace(/'/g, '"')
58
- .replace(/~TDQ~/g, "'")
59
- .replace(/\\'/g, "^'")
60
- )
61
- }
62
-
63
- static smartDecode<T = any>(val: any, schema: SchemaStructure): T {
64
- if (!val) return val as T
65
-
66
- if (Array.isArray(val)) // @ts-ignore
67
- return val.map((i: {v: string}) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
68
-
69
- return val?.V ? this.decode<T>(val.V, schema) : val
70
- }
71
-
72
- static decode<T = any>(val: string, schema: SchemaStructure): T {
73
- if (!val || typeof val !== 'string') return val as T
74
-
75
- return this.withSchema(this.unzip(JSON.parse(
76
- this.#unpack(val)
77
- .replace(/"/g, '~TSQ~')
78
- .replace(/'/g, '"')
79
- .replace(/~TSQ~/g, "'")
80
- .replace(/\^"/g, '\\"')
81
- .replace(/(?<=[,{\[]|^)(\^\d+)(?=[,\]}[]|$)/g, '"$1"')
82
- )), schema) as T
83
- }
84
-
85
- static zip(obj: any, schema: SchemaStructure, seen: any[]): any[] {
86
- if (Array.isArray(obj))
87
- return obj?.length ? obj.map(item => this.zip(item, schema, seen)) : []
88
-
89
- if (this.#cantZip(obj)) return obj
90
-
91
- return schema.map(key => {
92
- if (typeof key == 'string')
93
- return this.memo(obj[key], seen)
94
-
95
- const mainKey = Object.keys(key)[0]
96
- const subKeys = key[mainKey]
97
- const val = obj[mainKey]
98
-
99
- if (Array.isArray(val))
100
- return val.map(item => this.zip(item, subKeys, seen))
101
-
102
- return this.zip(val, subKeys, seen)
103
- })
104
- }
105
-
106
- static unzip(val: any, seen: any[] = []): any[] {
107
- if (Array.isArray(val))
108
- return val?.length ? val.map(item => this.unzip(item, seen)) : []
109
-
110
- const type = typeof val
111
- const length = getLength(val, type)
112
-
113
- if (this.#cantZip(val, type, length)) return val
114
-
115
- if (type == 'object') {
116
- for (const key in val)
117
- val[key] = this.unzip(val[key], seen)
118
-
119
- return val
120
- }
121
-
122
- if (type == 'string' && val.startsWith('^')) {
123
- const item = seen[parseInt(val.slice(1), 10)]
124
- return item ? item : val
125
- }
126
-
127
- seen.push(val)
128
- return val
129
- }
130
-
131
- static withSchema(value: any[], keys: any[], deep = false): any {
132
- if (!value || !Array.isArray(value))
133
- return value
134
-
135
- if (!deep && isArraySchema(keys))
136
- return value?.length ? value.map(v => this.withSchema(v, keys, true)) : []
137
-
138
- return Object.fromEntries(
139
- keys.map((key, index) => this.entry(key, value[index])).filter(Boolean)
140
- )
141
- }
142
-
143
- static entry(key: any, value: any): any {
144
- if (!key) return undefined
145
-
146
- if (typeof key == 'string')
147
- return [key, value === undefined ? null : value]
148
-
149
- const mainKey = Object.keys(key)[0]
150
- const subKeys = key[mainKey]
151
-
152
- if (Array.isArray(value)) {
153
- if (value.length < 1)
154
- return [mainKey, []]
155
-
156
- return Array.isArray(value[0])
157
- ? [mainKey, value.map(v => this.withSchema(v, subKeys))]
158
- : [mainKey, this.withSchema(value, subKeys)]
159
- }
160
-
161
- return [mainKey, value === undefined ? null : value]
162
- }
163
-
164
- static memo(val: any, seen: any[]): any {
165
- if (Array.isArray(val))
166
- return val.map(item => this.memo(item, seen))
167
-
168
- const type = typeof val
169
- if (type == 'object' && val != null) {
170
- for (const key in val)
171
- val[key] = this.memo(val[key], seen)
172
-
173
- return val
174
- }
175
-
176
- const length = getLength(val, type)
177
- if (this.#cantZip(val, type, length)) return val
178
-
179
- const index = seen.indexOf(val)
180
- if (index !== -1)
181
- return `^${index}`
182
-
183
- seen.push(val)
184
- return val
185
- }
186
-
187
- static #mapRegex(keys: string[]) {
188
- keys = keys.sort((a, b) => b.length - a.length).map(k => k.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
189
- return new RegExp(`(?<![^\\s,\\[\\{:])(${keys.join('|')})(?![^\\s,\\]\\}:])`, 'g')
190
- }
191
-
192
- static #pack(val: string): string {
193
- return val.replace(this.#typeRegex, match => this.#typeMap[match])
194
- }
195
-
196
- static #unpack(val: string): string {
197
- return val.replace(this.#reverseTypeRegex, match => this.#reverseTypeMap[match])
198
- }
199
-
200
- static #cantZip(val: any, type: string = '', length: number = 0) {
201
- if (!val || [null, true, false, 'true', 'false'].includes(val)) return true
202
-
203
- return !type && !length ? false : type != 'object' && length < 2
204
- }
205
- }
@@ -1,126 +0,0 @@
1
- import pluralize from 'pluralize'
2
- import type { ModelMetadata, ModelOpts } from './types'
3
-
4
- export function getModelMetadata(target: Function | any): ModelMetadata {
5
- if (!target?.m)
6
- throw Error(`Entity "${target?.name}" not registred, Use @Entity or @Model.`)
7
-
8
- const typeKeys = typeof target.m[1]
9
- return {
10
- table: target.m[0],
11
- // @ts-ignore
12
- keys: typeKeys != 'undefined' ? (typeKeys == 'string' ? { PK: target.m[1] } : { PK: target.m[1][0], SK: target.m[1][1] }) : undefined,
13
- defaultSK: target?.defaultSK || undefined,
14
- zip: target.m[2] || false,
15
- fields: target.m[3] || [],
16
- }
17
- }
18
-
19
- function _table(target: Function | any, opt?: ModelOpts) {
20
- if (!target?.m) target.m = []
21
- const table = opt ? (typeof opt == 'string' ? opt : opt?.table) : undefined
22
-
23
- target.m[0] = table || pluralize(target.name.toLocaleUpperCase())
24
- }
25
-
26
- function _zip(target: Function | any) {
27
- if (!target?.m) target.m = []
28
- target.m[2] = true
29
- target.m[3] = target?.schema || Object.keys(new target)
30
- }
31
-
32
- function _key(target: Function | any, pk: string, sk?: string) {
33
- if (!target?.m) target.m = []
34
- target.m[1] = pk && sk ? [pk, sk] : [pk]
35
- }
36
-
37
- export function _model(target: any, opt?: ModelOpts) {
38
- _table(target, opt)
39
- const notStr = typeof opt !== 'string'
40
-
41
- if (!opt || !notStr || (opt?.zip === undefined || opt?.zip))
42
- _zip(target)
43
-
44
- const pk = opt && notStr ? opt?.partitionKey : undefined
45
- const sk = opt && notStr ? opt?.sortKey : undefined
46
- _key(target, pk || 'PK', sk || 'SK')
47
- }
48
-
49
- function _pk(target: any, prop: string) {
50
- if (!target?.m) target.m = []
51
- if (['string', 'undefined'].includes(typeof target.m[1])) {
52
- target.m[1] = prop
53
- } else {
54
- target.m[1][0] = prop
55
- }
56
- }
57
-
58
- function _sk(target: any, prop: string) {
59
- if (!target?.m) target.m = []
60
- if (['string', 'undefined'].includes(typeof target.m[1])) {
61
- target.m[1] = []
62
- target.m[1][1] = prop
63
- } else {
64
- target.m[1][0] = prop
65
- }
66
- }
67
-
68
- export function Entity(target: Function): void
69
- export function Entity(opt?: ModelOpts): ClassDecorator
70
- export function Entity(...args: any[]): void | ClassDecorator {
71
- if (args.length == 1 && typeof args[0] == 'function')
72
- return _table(args[0])
73
-
74
- return (target: any) => _table(target, ...args)
75
- }
76
-
77
- export function Model(target: Function): void
78
- export function Model(opt?: ModelOpts): ClassDecorator
79
- export function Model(...args: any[]): void | ClassDecorator {
80
- if (args.length == 1 && typeof args[0] == 'function')
81
- return _model(args[0])
82
-
83
- return (target: any) => _model(target, ...args)
84
- }
85
-
86
- export function Zip(target: Function): void
87
- export function Zip(): ClassDecorator
88
- export function Zip(...args: any[]): void | ClassDecorator {
89
- if (args.length == 1 && typeof args[0] == 'function')
90
- return _zip(args[0])
91
-
92
- return (target: any) => _zip(target)
93
- }
94
-
95
- export function Key(pk: string, sk?: string) {
96
- return (target: any) => {
97
- _key(target, pk, sk)
98
- }
99
- }
100
- export const Keys = Key
101
-
102
- export function PartitionKey(attrName: string): PropertyDecorator
103
- export function PartitionKey(target: any, propertyKey: string): void
104
- export function PartitionKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
105
- export function PartitionKey(...args: any[]): void | PropertyDecorator {
106
- if (!args.length) return
107
-
108
- if (typeof args[0] == 'function' && typeof args[1] == 'string' && args[1])
109
- return _pk(args[0], args[1])
110
-
111
- if (args.length == 1 && args[0])
112
- return (target: any) => _pk(target, args[0])
113
- }
114
-
115
- export function SortKey(attrName: string): PropertyDecorator
116
- export function SortKey(target: any, propertyKey: string): void
117
- export function SortKey(target: any, propertyKey: string | undefined, parameterIndex: number): void
118
- export function SortKey(...args: any[]): void | PropertyDecorator {
119
- if (!args.length) return
120
-
121
- if (typeof args[0] == 'function' && typeof args[1] == 'string' && args[1])
122
- return _sk(args[0], args[1])
123
-
124
- if (args.length == 1 && args[0])
125
- return (target: any) => _sk(target, args[0])
126
- }
@@ -1,4 +0,0 @@
1
- export { Dynamodb, DocumentClient, RawClient } from './client'
2
- export { Model, Entity, Zip, PartitionKey, SortKey, Key, Keys } from './decorators'
3
- export { Schema } from './schema'
4
- export { Repository } from './repository'