velocious 1.0.104 → 1.0.105
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/package.json +1 -1
- package/spec/dummy/dummy-directory.js +2 -0
- package/spec/dummy/index.js +5 -1
- package/src/application.js +1 -0
- package/src/configuration-types.js +6 -0
- package/src/controller.js +44 -24
- package/src/database/drivers/base-foreign-key.js +1 -1
- package/src/database/drivers/base.js +2 -2
- package/src/database/drivers/mssql/column.js +6 -0
- package/src/database/drivers/mssql/columns-index.js +2 -5
- package/src/database/drivers/mssql/foreign-key.js +2 -0
- package/src/database/drivers/mssql/options.js +25 -0
- package/src/database/drivers/mssql/query-parser.js +2 -0
- package/src/database/drivers/mysql/options.js +9 -0
- package/src/database/drivers/mysql/sql/alter-table.js +2 -0
- package/src/database/drivers/mysql/sql/create-database.js +2 -0
- package/src/database/drivers/mysql/sql/create-index.js +2 -0
- package/src/database/drivers/mysql/sql/create-table.js +2 -0
- package/src/database/drivers/mysql/sql/delete.js +2 -0
- package/src/database/drivers/mysql/sql/drop-table.js +2 -0
- package/src/database/drivers/mysql/sql/insert.js +2 -0
- package/src/database/drivers/mysql/sql/update.js +2 -0
- package/src/database/drivers/pgsql/column.js +6 -0
- package/src/database/drivers/pgsql/columns-index.js +2 -0
- package/src/database/drivers/pgsql/foreign-key.js +2 -0
- package/src/database/drivers/pgsql/options.js +9 -0
- package/src/database/drivers/pgsql/query-parser.js +2 -0
- package/src/database/drivers/pgsql/sql/alter-table.js +2 -0
- package/src/database/drivers/pgsql/sql/create-database.js +5 -4
- package/src/database/drivers/pgsql/sql/create-index.js +2 -0
- package/src/database/drivers/pgsql/sql/create-table.js +2 -0
- package/src/database/drivers/pgsql/sql/delete.js +2 -0
- package/src/database/drivers/pgsql/sql/drop-table.js +2 -0
- package/src/database/drivers/pgsql/sql/insert.js +2 -0
- package/src/database/drivers/pgsql/sql/update.js +2 -0
- package/src/database/drivers/pgsql/table.js +6 -0
- package/src/database/drivers/sqlite/columns-index.js +2 -6
- package/src/database/drivers/sqlite/connection-remote.js +7 -0
- package/src/database/drivers/sqlite/connection-sql-js.js +12 -2
- package/src/database/drivers/sqlite/foreign-key.js +7 -0
- package/src/database/drivers/sqlite/index.js +7 -1
- package/src/database/drivers/sqlite/index.web.js +12 -3
- package/src/database/drivers/sqlite/options.js +9 -0
- package/src/database/drivers/sqlite/query-parser.js +2 -0
- package/src/database/drivers/sqlite/query.js +19 -6
- package/src/database/drivers/sqlite/query.web.js +13 -1
- package/src/database/initializer-from-require-context.js +11 -1
- package/src/database/migrator/types.js +2 -0
- package/src/database/pool/base-methods-forward.js +7 -0
- package/src/database/query/delete-base.js +8 -0
- package/src/database/query/preloader/belongs-to.js +16 -1
- package/src/database/query/preloader/has-many.js +19 -1
- package/src/database/query/preloader/has-one.js +20 -2
- package/src/database/query/preloader.js +19 -4
- package/src/database/query/update-base.js +9 -0
- package/src/database/query-parser/limit-parser.js +7 -2
- package/src/database/query-parser/options.js +47 -6
- package/src/database/query-parser/order-parser.js +11 -6
- package/src/database/query-parser/select-parser.js +8 -5
- package/src/database/query-parser/where-parser.js +11 -5
- package/src/database/record/index.js +1 -4
- package/src/database/record/instance-relationships/base.js +10 -1
- package/src/database/record/record-not-found-error.js +2 -0
- package/src/database/record/user-module.js +13 -0
- package/src/database/record/validators/uniqueness.js +13 -2
- package/src/error-logger.js +17 -3
- package/src/http-client/index.js +34 -2
- package/src/http-client/request.js +1 -1
- package/src/http-server/client/params-to-object.js +28 -0
- package/src/initializer.js +2 -0
- package/src/routes/app-routes.js +3 -1
- package/src/routes/base-route.js +67 -58
- package/src/routes/basic-route.js +76 -0
- package/src/routes/get-route.js +21 -5
- package/src/routes/index.js +10 -0
- package/src/routes/namespace-route.js +21 -5
- package/src/routes/post-route.js +20 -5
- package/src/routes/resolver.js +15 -2
- package/src/routes/resource-route.js +21 -5
- package/src/routes/root-route.js +3 -3
- package/src/testing/request-client.js +19 -14
- package/src/testing/test-runner.js +16 -10
- package/src/testing/test.js +70 -22
- package/src/utils/with-tracked-stack-async-hooks.js +22 -4
- package/src/utils/with-tracked-stack.js +9 -0
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
// @ts-check
|
|
2
2
|
|
|
3
3
|
export default class VelocuiousDatabaseQueryParserWhereParser {
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} args
|
|
6
|
+
* @param {boolean} args.pretty
|
|
7
|
+
* @param {import("../query/index.js").default} args.query
|
|
8
|
+
*/
|
|
4
9
|
constructor({pretty, query}) {
|
|
5
10
|
this.pretty = pretty
|
|
6
11
|
this.query = query
|
|
7
12
|
}
|
|
8
13
|
|
|
9
14
|
toSql() {
|
|
10
|
-
const {pretty, query} =
|
|
15
|
+
const {pretty, query} = this
|
|
11
16
|
let sql = ""
|
|
12
17
|
|
|
13
18
|
if (query._wheres.length == 0) return sql
|
|
@@ -20,10 +25,10 @@ export default class VelocuiousDatabaseQueryParserWhereParser {
|
|
|
20
25
|
|
|
21
26
|
sql += "WHERE"
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
const where = query._wheres[whereKey]
|
|
28
|
+
let count = 0
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
for (const where of query._wheres) {
|
|
31
|
+
if (count > 0) sql += " AND"
|
|
27
32
|
|
|
28
33
|
if (pretty) {
|
|
29
34
|
sql += "\n "
|
|
@@ -32,6 +37,7 @@ export default class VelocuiousDatabaseQueryParserWhereParser {
|
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
sql += where.toSql()
|
|
40
|
+
count++
|
|
35
41
|
}
|
|
36
42
|
|
|
37
43
|
return sql
|
|
@@ -628,10 +628,7 @@ class VelociousDatabaseRecord {
|
|
|
628
628
|
return this._attributes[columnName]
|
|
629
629
|
}
|
|
630
630
|
|
|
631
|
-
/**
|
|
632
|
-
*
|
|
633
|
-
* @returns {typeof VelociousDatabaseRecord}
|
|
634
|
-
*/
|
|
631
|
+
/** @returns {typeof VelociousDatabaseRecord} */
|
|
635
632
|
getModelClass() {
|
|
636
633
|
const modelClass = /** @type {typeof VelociousDatabaseRecord} */ (this.constructor)
|
|
637
634
|
|
|
@@ -15,6 +15,15 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
|
|
|
15
15
|
this.relationship = relationship
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @abstract
|
|
20
|
+
* @param {import("../index.js").default[]} records
|
|
21
|
+
* @returns {void}
|
|
22
|
+
*/
|
|
23
|
+
addToLoaded(records) { // eslint-disable-line no-unused-vars
|
|
24
|
+
throw new Error("addToLoaded not implemented")
|
|
25
|
+
}
|
|
26
|
+
|
|
18
27
|
/**
|
|
19
28
|
* @abstract
|
|
20
29
|
* @param {Record<string, any>} attributes
|
|
@@ -62,7 +71,7 @@ export default class VelociousDatabaseRecordBaseInstanceRelationship {
|
|
|
62
71
|
return this._loaded
|
|
63
72
|
}
|
|
64
73
|
|
|
65
|
-
/** @param {import("../index.js").default|Array<import("../index.js").default
|
|
74
|
+
/** @param {import("../index.js").default|Array<import("../index.js").default>|undefined} model */
|
|
66
75
|
setLoaded(model) { this._loaded = model }
|
|
67
76
|
|
|
68
77
|
/** @returns {import("../index.js").default | import("../index.js").default[] | undefined} */
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import bcryptjs from "bcryptjs"
|
|
2
4
|
import restArgsError from "../../utils/rest-args-error.js"
|
|
3
5
|
|
|
4
6
|
export default class UserModule {
|
|
7
|
+
/**
|
|
8
|
+
* @param {object} args
|
|
9
|
+
* @param {string} args.secretKey
|
|
10
|
+
*/
|
|
5
11
|
constructor({secretKey, ...restArgs}) {
|
|
6
12
|
restArgsError(restArgs)
|
|
7
13
|
|
|
@@ -10,18 +16,25 @@ export default class UserModule {
|
|
|
10
16
|
this.secretKey = secretKey
|
|
11
17
|
}
|
|
12
18
|
|
|
19
|
+
/**
|
|
20
|
+
* @param {typeof import("./index.js").default} UserClass
|
|
21
|
+
*/
|
|
13
22
|
attachTo(UserClass) {
|
|
23
|
+
// @ts-expect-error
|
|
14
24
|
UserClass.prototype.setPassword = function(newPassword) {
|
|
15
25
|
const salt = bcryptjs.genSaltSync(10)
|
|
16
26
|
const encryptedPassword = bcryptjs.hashSync(newPassword, salt)
|
|
17
27
|
|
|
28
|
+
// @ts-expect-error
|
|
18
29
|
this.setEncryptedPassword(encryptedPassword)
|
|
19
30
|
}
|
|
20
31
|
|
|
32
|
+
// @ts-expect-error
|
|
21
33
|
UserClass.prototype.setPasswordConfirmation = function(newPasswordConfirmation) {
|
|
22
34
|
const salt = bcryptjs.genSaltSync(10)
|
|
23
35
|
const encryptedPassword = bcryptjs.hashSync(newPasswordConfirmation, salt)
|
|
24
36
|
|
|
37
|
+
// @ts-expect-error
|
|
25
38
|
this._encryptedPasswordConfirmation = encryptedPassword
|
|
26
39
|
}
|
|
27
40
|
}
|
|
@@ -1,18 +1,29 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import Base from "./base.js"
|
|
2
4
|
import * as inflection from "inflection"
|
|
3
5
|
|
|
4
6
|
export default class VelociousDatabaseRecordValidatorsUniqueness extends Base {
|
|
7
|
+
/**
|
|
8
|
+
* @param {object} args
|
|
9
|
+
* @param {import("../index.js").default} args.model
|
|
10
|
+
* @param {string} args.attributeName
|
|
11
|
+
* @returns {Promise<void>}
|
|
12
|
+
*/
|
|
5
13
|
async validate({model, attributeName}) {
|
|
6
|
-
const modelClass = model.constructor
|
|
14
|
+
const modelClass = /** @type {typeof import("../index.js").default} */ (model.constructor)
|
|
15
|
+
|
|
7
16
|
const connection = modelClass.connection()
|
|
8
17
|
const tableName = modelClass._getTable().getName()
|
|
9
18
|
const attributeValue = model.readAttribute(attributeName)
|
|
10
19
|
const attributeNameUnderscore = inflection.underscore(attributeName)
|
|
20
|
+
|
|
21
|
+
/** @type {Record<string, any>} */
|
|
11
22
|
const whereArgs = {}
|
|
12
23
|
|
|
13
24
|
whereArgs[attributeNameUnderscore] = attributeValue
|
|
14
25
|
|
|
15
|
-
let existingRecordQuery =
|
|
26
|
+
let existingRecordQuery = modelClass
|
|
16
27
|
.select(modelClass.primaryKey())
|
|
17
28
|
.where(whereArgs)
|
|
18
29
|
|
package/src/error-logger.js
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @param {(...args: any[]) => Promise<void>} callback
|
|
5
|
+
*/
|
|
1
6
|
export default function errorLogger(callback) {
|
|
7
|
+
/**
|
|
8
|
+
* @param {...any} args
|
|
9
|
+
* @returns {Promise<void>}
|
|
10
|
+
*/
|
|
2
11
|
return async function(...args) {
|
|
3
12
|
try {
|
|
4
13
|
await callback(...args)
|
|
5
14
|
} catch (error) {
|
|
6
|
-
|
|
15
|
+
if (error instanceof Error) {
|
|
16
|
+
console.error(`ErrorLogger: ${error.message}`)
|
|
7
17
|
|
|
8
|
-
|
|
9
|
-
|
|
18
|
+
if (error.stack) {
|
|
19
|
+
console.error("Stack", error.stack)
|
|
20
|
+
} else {
|
|
21
|
+
console.error("No stack")
|
|
22
|
+
}
|
|
10
23
|
} else {
|
|
24
|
+
console.error(`ErrorLogger: ${error}`)
|
|
11
25
|
console.error("No stack")
|
|
12
26
|
}
|
|
13
27
|
|
package/src/http-client/index.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import net from "net"
|
|
2
4
|
import Request from "./request.js"
|
|
3
5
|
import Response from "./response.js"
|
|
4
6
|
import {Logger} from "../logger.js"
|
|
5
7
|
|
|
6
8
|
export default class HttpClient {
|
|
9
|
+
/**
|
|
10
|
+
* @param {object} args
|
|
11
|
+
* @param {boolean} [args.debug]
|
|
12
|
+
* @param {Array<import("./header.js").default>} [args.headers]
|
|
13
|
+
* @param {string} [args.version]
|
|
14
|
+
*/
|
|
7
15
|
constructor({debug = false, headers, version = "1.1"}) {
|
|
8
16
|
this.headers = headers || []
|
|
9
17
|
this.logger = new Logger(this, {debug})
|
|
@@ -15,7 +23,7 @@ export default class HttpClient {
|
|
|
15
23
|
this.connectionReject = reject
|
|
16
24
|
this.connection = net.createConnection(3006, "127.0.0.1", () => {
|
|
17
25
|
this.connectionReject = null
|
|
18
|
-
resolve()
|
|
26
|
+
resolve(null)
|
|
19
27
|
})
|
|
20
28
|
|
|
21
29
|
this.connection.on("data", this.onConnectionData)
|
|
@@ -24,6 +32,12 @@ export default class HttpClient {
|
|
|
24
32
|
})
|
|
25
33
|
}
|
|
26
34
|
|
|
35
|
+
/**
|
|
36
|
+
* @param {string} path
|
|
37
|
+
* @param {object} [options]
|
|
38
|
+
* @param {Array<import("./header.js").default>} [options.headers]
|
|
39
|
+
* @returns {Promise<{request: import("./request.js").default, response: import("./response.js").default}>}
|
|
40
|
+
*/
|
|
27
41
|
get(path, {headers} = {}) {
|
|
28
42
|
if (!this.connection) throw new Error("Not connected yet")
|
|
29
43
|
|
|
@@ -41,7 +55,7 @@ export default class HttpClient {
|
|
|
41
55
|
|
|
42
56
|
for (const header of this.headers) {
|
|
43
57
|
const existingNewHeader = newHeaders.find((newHeader) => {
|
|
44
|
-
return newHeader.
|
|
58
|
+
return newHeader.getName().toLowerCase().trim() === header.getName().toLowerCase().trim()
|
|
45
59
|
})
|
|
46
60
|
|
|
47
61
|
if (!existingNewHeader) {
|
|
@@ -56,8 +70,14 @@ export default class HttpClient {
|
|
|
56
70
|
this.currentRequest.stream((chunk) => {
|
|
57
71
|
this.logger.debug(() => [`Writing: ${chunk}`])
|
|
58
72
|
|
|
73
|
+
if (!this.connection) {
|
|
74
|
+
throw new Error("No connection to write to")
|
|
75
|
+
}
|
|
76
|
+
|
|
59
77
|
this.connection.write(chunk, "utf8", (error) => {
|
|
60
78
|
if (error) {
|
|
79
|
+
if (!this.currentRequestReject) throw new Error("No current request reject function")
|
|
80
|
+
|
|
61
81
|
this.currentRequestReject(error)
|
|
62
82
|
}
|
|
63
83
|
})
|
|
@@ -65,7 +85,12 @@ export default class HttpClient {
|
|
|
65
85
|
})
|
|
66
86
|
}
|
|
67
87
|
|
|
88
|
+
/**
|
|
89
|
+
* @param {Buffer} data
|
|
90
|
+
*/
|
|
68
91
|
onConnectionData = (data) => {
|
|
92
|
+
if (!this.currentResponse) throw new Error("No current response to feed data to")
|
|
93
|
+
|
|
69
94
|
this.currentResponse.feed(data)
|
|
70
95
|
}
|
|
71
96
|
|
|
@@ -73,6 +98,9 @@ export default class HttpClient {
|
|
|
73
98
|
this.connection = null
|
|
74
99
|
}
|
|
75
100
|
|
|
101
|
+
/**
|
|
102
|
+
* @param {Error} error
|
|
103
|
+
*/
|
|
76
104
|
onConnectionError = (error) => {
|
|
77
105
|
if (this.connectionReject) {
|
|
78
106
|
this.connectionReject(error)
|
|
@@ -90,6 +118,10 @@ export default class HttpClient {
|
|
|
90
118
|
}
|
|
91
119
|
|
|
92
120
|
onResponseComplete = () => {
|
|
121
|
+
if (!this.currentRequestResolve) throw new Error("No current request resolve function")
|
|
122
|
+
if (!this.currentRequest) throw new Error("No current request")
|
|
123
|
+
if (!this.currentResponse) throw new Error("No current response")
|
|
124
|
+
|
|
93
125
|
this.currentRequestResolve({
|
|
94
126
|
request: this.currentRequest,
|
|
95
127
|
response: this.currentResponse
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
export default class ParamsToObject {
|
|
4
|
+
/**
|
|
5
|
+
* @param {Record<string, string>} object
|
|
6
|
+
*/
|
|
2
7
|
constructor(object) {
|
|
3
8
|
this.object = object
|
|
4
9
|
}
|
|
@@ -15,6 +20,11 @@ export default class ParamsToObject {
|
|
|
15
20
|
return result
|
|
16
21
|
}
|
|
17
22
|
|
|
23
|
+
/**
|
|
24
|
+
* @param {string} key
|
|
25
|
+
* @param {string} value
|
|
26
|
+
* @param {Record<string, any> | any[]} result
|
|
27
|
+
*/
|
|
18
28
|
treatInitial(key, value, result) {
|
|
19
29
|
const firstMatch = key.match(/^(.+?)(\[([\s\S]+$))/)
|
|
20
30
|
|
|
@@ -22,43 +32,61 @@ export default class ParamsToObject {
|
|
|
22
32
|
const inputName = firstMatch[1]
|
|
23
33
|
const rest = firstMatch[2]
|
|
24
34
|
|
|
35
|
+
/** @type {Array<any> | Record<string, any>} */
|
|
25
36
|
let newResult
|
|
26
37
|
|
|
27
38
|
if (inputName in result) {
|
|
39
|
+
// @ts-expect-error
|
|
28
40
|
newResult = result[inputName]
|
|
29
41
|
} else if (rest == "[]") {
|
|
30
42
|
newResult = []
|
|
43
|
+
// @ts-expect-error
|
|
31
44
|
result[inputName] = newResult
|
|
32
45
|
} else {
|
|
33
46
|
newResult = {}
|
|
47
|
+
// @ts-expect-error
|
|
34
48
|
result[inputName] = newResult
|
|
35
49
|
}
|
|
36
50
|
|
|
37
51
|
this.treatSecond(value, rest, newResult)
|
|
38
52
|
} else {
|
|
53
|
+
// @ts-expect-error
|
|
39
54
|
result[key] = value
|
|
40
55
|
}
|
|
41
56
|
}
|
|
42
57
|
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} value
|
|
60
|
+
* @param {string} rest
|
|
61
|
+
* @param {Record<string, any> | any[]} result
|
|
62
|
+
*/
|
|
43
63
|
treatSecond(value, rest, result) {
|
|
44
64
|
const secondMatch = rest.match(/^\[(.*?)\]([\s\S]*)$/)
|
|
65
|
+
|
|
66
|
+
if (!secondMatch) throw new Error(`Could not parse rest part: ${rest}`)
|
|
67
|
+
|
|
45
68
|
const key = secondMatch[1]
|
|
46
69
|
const newRest = secondMatch[2]
|
|
47
70
|
|
|
71
|
+
/** @type {Array<any> | Record<string, any>} */
|
|
48
72
|
let newResult
|
|
49
73
|
|
|
50
74
|
if (rest == "[]") {
|
|
51
75
|
result.push(value)
|
|
52
76
|
} else if (newRest == "") {
|
|
77
|
+
// @ts-expect-error
|
|
53
78
|
result[key] = value
|
|
54
79
|
} else {
|
|
55
80
|
if (typeof result == "object" && key in result) {
|
|
81
|
+
// @ts-expect-error
|
|
56
82
|
newResult = result[key]
|
|
57
83
|
} else if (newRest == "[]") {
|
|
58
84
|
newResult = []
|
|
85
|
+
// @ts-expect-error
|
|
59
86
|
result[key] = newResult
|
|
60
87
|
} else {
|
|
61
88
|
newResult = {}
|
|
89
|
+
// @ts-expect-error
|
|
62
90
|
result[key] = newResult
|
|
63
91
|
}
|
|
64
92
|
|
package/src/initializer.js
CHANGED
package/src/routes/app-routes.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import {digg} from "diggerize"
|
|
2
4
|
|
|
3
5
|
export default class VelociousRoutesAppRoutes {
|
|
4
6
|
/**
|
|
5
7
|
* @param {import("../configuration.js").default} configuration
|
|
6
|
-
* @returns {import("./index.js").default}
|
|
8
|
+
* @returns {Promise<import("./index.js").default>}
|
|
7
9
|
*/
|
|
8
10
|
static async getRoutes(configuration) {
|
|
9
11
|
// Every client need to make their own routes because they probably can't be shared across different worker threads
|
package/src/routes/base-route.js
CHANGED
|
@@ -1,73 +1,82 @@
|
|
|
1
|
-
|
|
2
|
-
import NamespaceRoute from "./namespace-route.js"
|
|
3
|
-
import PostRoute from "./post-route.js"
|
|
4
|
-
import ResourceRoute from "./resource-route.js"
|
|
1
|
+
// @ts-check
|
|
5
2
|
|
|
6
|
-
|
|
3
|
+
export default class VelociousBaseRoute {
|
|
4
|
+
/** @type {typeof import("./get-route.js").default} */
|
|
5
|
+
static GetRouteType
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
/** @type {typeof import("./namespace-route.js").default} */
|
|
8
|
+
static NameSpaceRouteType
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
/** @type {typeof import("./post-route.js").default} */
|
|
11
|
+
static PostRouteType
|
|
13
12
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
get(name) {
|
|
18
|
-
const route = new GetRoute({name})
|
|
13
|
+
/** @type {typeof import("./resource-route.js").default} */
|
|
14
|
+
static ResourceRouteType
|
|
19
15
|
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
/** @param {typeof import("./get-route.js").default} RouteClass */
|
|
17
|
+
static registerRouteGetType(RouteClass) {
|
|
18
|
+
this.GetRouteType = RouteClass
|
|
19
|
+
}
|
|
22
20
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
matchWithPath(_path) { // eslint-disable-line no-unused-vars
|
|
28
|
-
throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
|
|
29
|
-
}
|
|
21
|
+
/** @param {typeof import("./namespace-route.js").default} RouteClass */
|
|
22
|
+
static registerRouteNamespaceType(RouteClass) {
|
|
23
|
+
this.NameSpaceRouteType = RouteClass
|
|
24
|
+
}
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
*/
|
|
36
|
-
namespace(name, callback) {
|
|
37
|
-
const route = new NamespaceRoute({name})
|
|
26
|
+
/** @param {typeof import("./post-route.js").default} RouteClass */
|
|
27
|
+
static registerRoutePostType(RouteClass) {
|
|
28
|
+
this.PostRouteType = RouteClass
|
|
29
|
+
}
|
|
38
30
|
|
|
39
|
-
|
|
31
|
+
/** @param {typeof import("./resource-route.js").default} RouteClass */
|
|
32
|
+
static registerRouteResourceType(RouteClass) {
|
|
33
|
+
this.ResourceRouteType = RouteClass
|
|
34
|
+
}
|
|
40
35
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
36
|
+
constructor() {
|
|
37
|
+
// Nothing
|
|
38
|
+
}
|
|
45
39
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
* @returns {void}
|
|
49
|
-
*/
|
|
50
|
-
post(name) {
|
|
51
|
-
const route = new PostRoute({name})
|
|
40
|
+
/** @type {Array<VelociousBaseRoute>} */
|
|
41
|
+
routes = []
|
|
52
42
|
|
|
53
|
-
|
|
54
|
-
|
|
43
|
+
/**
|
|
44
|
+
* @abstract
|
|
45
|
+
* @param {string} name
|
|
46
|
+
*/
|
|
47
|
+
get(name) { throw new Error("'get' not implemented") } // eslint-disable-line no-unused-vars
|
|
55
48
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
49
|
+
/**
|
|
50
|
+
* @param {object} args
|
|
51
|
+
* @param {Record<string, any>} args.params
|
|
52
|
+
* @param {string} args.path
|
|
53
|
+
* @param {import("../http-server/client/request.js").default} args.request
|
|
54
|
+
* @returns {{restPath: string} | undefined}
|
|
55
|
+
*/
|
|
56
|
+
matchWithPath({params, path, request}) { // eslint-disable-line no-unused-vars
|
|
57
|
+
throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
|
|
58
|
+
}
|
|
63
59
|
|
|
64
|
-
|
|
60
|
+
/**
|
|
61
|
+
* @abstract
|
|
62
|
+
* @param {string} name
|
|
63
|
+
* @param {function(import("./namespace-route.js").default) : void} callback
|
|
64
|
+
* @returns {void}
|
|
65
|
+
*/
|
|
66
|
+
namespace(name, callback) { throw new Error("'namespace' not implemented") } // eslint-disable-line no-unused-vars
|
|
65
67
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
68
|
+
/**
|
|
69
|
+
* @abstract
|
|
70
|
+
* @param {string} name
|
|
71
|
+
* @returns {void}
|
|
72
|
+
*/
|
|
73
|
+
post(name) { throw new Error("'post' not implemented") } // eslint-disable-line no-unused-vars
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
/**
|
|
76
|
+
* @abstract
|
|
77
|
+
* @param {string} name
|
|
78
|
+
* @param {function(import("./resource-route.js").default) : void} callback
|
|
79
|
+
* @returns {void}
|
|
80
|
+
*/
|
|
81
|
+
resources(name, callback) { throw new Error("'resources' not implemented") } // eslint-disable-line no-unused-vars
|
|
82
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import BaseRoute from "./base-route.js"
|
|
4
|
+
|
|
5
|
+
export default class VelociousBasicRoute extends BaseRoute {
|
|
6
|
+
/** @param {string} name */
|
|
7
|
+
get(name) {
|
|
8
|
+
const GetRoute = VelociousBasicRoute.GetRouteType
|
|
9
|
+
const route = new GetRoute({name})
|
|
10
|
+
|
|
11
|
+
this.routes.push(route)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {object} args
|
|
16
|
+
* @param {Record<string, any>} args.params
|
|
17
|
+
* @param {string} args.path
|
|
18
|
+
* @param {import("../http-server/client/request.js").default} args.request
|
|
19
|
+
* @returns {{restPath: string} | undefined}
|
|
20
|
+
*/
|
|
21
|
+
matchWithPath({params, path, request}) { // eslint-disable-line no-unused-vars
|
|
22
|
+
throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} name
|
|
27
|
+
* @param {function(import("./namespace-route.js").default) : void} callback
|
|
28
|
+
* @returns {void}
|
|
29
|
+
*/
|
|
30
|
+
namespace(name, callback) {
|
|
31
|
+
const NamespaceRoute = VelociousBasicRoute.NameSpaceRouteType
|
|
32
|
+
|
|
33
|
+
if (!NamespaceRoute) throw new Error("No NamespaceRoute registered")
|
|
34
|
+
|
|
35
|
+
const route = new NamespaceRoute({name})
|
|
36
|
+
|
|
37
|
+
this.routes.push(route)
|
|
38
|
+
|
|
39
|
+
if (callback) {
|
|
40
|
+
callback(route)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {string} name
|
|
46
|
+
* @returns {void}
|
|
47
|
+
*/
|
|
48
|
+
post(name) {
|
|
49
|
+
const PostRoute = VelociousBasicRoute.PostRouteType
|
|
50
|
+
|
|
51
|
+
if (!PostRoute) throw new Error("No PostRoute registered")
|
|
52
|
+
|
|
53
|
+
const route = new PostRoute({name})
|
|
54
|
+
|
|
55
|
+
this.routes.push(route)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} name
|
|
60
|
+
* @param {function(import("./resource-route.js").default) : void} callback
|
|
61
|
+
* @returns {void}
|
|
62
|
+
*/
|
|
63
|
+
resources(name, callback) {
|
|
64
|
+
const ResourceRoute = VelociousBasicRoute.ResourceRouteType
|
|
65
|
+
|
|
66
|
+
if (!ResourceRoute) throw new Error("No ResourceRoute registered")
|
|
67
|
+
|
|
68
|
+
const route = new ResourceRoute({name})
|
|
69
|
+
|
|
70
|
+
this.routes.push(route)
|
|
71
|
+
|
|
72
|
+
if (callback) {
|
|
73
|
+
callback(route)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/routes/get-route.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
2
3
|
import escapeStringRegexp from "escape-string-regexp"
|
|
3
|
-
import restArgsError from "../utils/rest-args-error.js"
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import BaseRoute from "./base-route.js"
|
|
6
|
+
import restArgsError from "../utils/rest-args-error.js"
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
class VelociousRouteGetRoute extends BaseRoute {
|
|
9
|
+
/**
|
|
10
|
+
* @param {object} args
|
|
11
|
+
* @param {string} args.name
|
|
12
|
+
*/
|
|
8
13
|
constructor({name, ...restArgs}) {
|
|
9
14
|
super()
|
|
10
15
|
restArgsError(restArgs)
|
|
@@ -12,7 +17,14 @@ export default class VelociousRouteGetRoute extends BaseRoute {
|
|
|
12
17
|
this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
|
|
13
18
|
}
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
/**
|
|
21
|
+
* @param {object} args
|
|
22
|
+
* @param {Record<string, any>} args.params
|
|
23
|
+
* @param {string} args.path
|
|
24
|
+
* @param {import("../http-server/client/request.js").default} args.request
|
|
25
|
+
* @returns {{restPath: string} | undefined}
|
|
26
|
+
*/
|
|
27
|
+
matchWithPath({params, path, request}) { // eslint-disable-line no-unused-vars
|
|
16
28
|
const match = path.match(this.regExp)
|
|
17
29
|
|
|
18
30
|
if (match) {
|
|
@@ -24,3 +36,7 @@ export default class VelociousRouteGetRoute extends BaseRoute {
|
|
|
24
36
|
}
|
|
25
37
|
}
|
|
26
38
|
}
|
|
39
|
+
|
|
40
|
+
BaseRoute.registerRouteGetType(VelociousRouteGetRoute)
|
|
41
|
+
|
|
42
|
+
export default VelociousRouteGetRoute
|
package/src/routes/index.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import GetRoute from "./get-route.js" // eslint-disable-line no-unused-vars
|
|
4
|
+
import NameSpaceRoute from "./namespace-route.js" // eslint-disable-line no-unused-vars
|
|
5
|
+
import PostRoute from "./post-route.js" // eslint-disable-line no-unused-vars
|
|
1
6
|
import RootRoute from "./root-route.js"
|
|
7
|
+
import ResourceRoute from "./resource-route.js" // eslint-disable-line no-unused-vars
|
|
2
8
|
|
|
3
9
|
export default class VelociousRoutes {
|
|
4
10
|
rootRoute = new RootRoute()
|
|
5
11
|
|
|
12
|
+
/**
|
|
13
|
+
* @param {function(import("./root-route.js").default) : void} callback
|
|
14
|
+
* @returns {void}
|
|
15
|
+
*/
|
|
6
16
|
draw(callback) {
|
|
7
17
|
callback(this.rootRoute)
|
|
8
18
|
}
|