@point-hub/papi 0.1.6 → 0.1.8

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.
Files changed (71) hide show
  1. package/lib/src/app.d.ts +3 -0
  2. package/lib/src/app.d.ts.map +1 -0
  3. package/lib/src/app.spec.d.ts +2 -0
  4. package/lib/src/app.spec.d.ts.map +1 -0
  5. package/lib/src/console/commands/make-command/index.command.d.ts +6 -0
  6. package/lib/src/console/commands/make-command/index.command.d.ts.map +1 -0
  7. package/lib/src/console/commands/make-command/index.spec.d.ts +2 -0
  8. package/lib/src/console/commands/make-command/index.spec.d.ts.map +1 -0
  9. package/lib/src/console/commands/make-middleware/index.command.d.ts +8 -0
  10. package/lib/src/console/commands/make-middleware/index.command.d.ts.map +1 -0
  11. package/lib/src/console/commands/make-middleware/index.spec.d.ts +2 -0
  12. package/lib/src/console/commands/make-middleware/index.spec.d.ts.map +1 -0
  13. package/lib/src/console/index.d.ts +13 -0
  14. package/lib/src/console/index.d.ts.map +1 -0
  15. package/lib/src/console/index.spec.d.ts +2 -0
  16. package/lib/src/console/index.spec.d.ts.map +1 -0
  17. package/lib/src/database/connection.d.ts +33 -0
  18. package/lib/src/database/connection.d.ts.map +1 -0
  19. package/lib/src/database/mongodb/connection.d.ts +38 -0
  20. package/lib/src/database/mongodb/connection.d.ts.map +1 -0
  21. package/lib/src/database/mongodb/mongodb-error-handler.d.ts +10 -0
  22. package/lib/src/database/mongodb/mongodb-error-handler.d.ts.map +1 -0
  23. package/lib/src/database/mongodb/mongodb-helper.d.ts +36 -0
  24. package/lib/src/database/mongodb/mongodb-helper.d.ts.map +1 -0
  25. package/lib/src/database/mongodb/mongodb-querystring.d.ts +82 -0
  26. package/lib/src/database/mongodb/mongodb-querystring.d.ts.map +1 -0
  27. package/lib/src/database/mongodb/mongodb-querystring.spec.d.ts +2 -0
  28. package/lib/src/database/mongodb/mongodb-querystring.spec.d.ts.map +1 -0
  29. package/lib/src/index.d.ts +39 -0
  30. package/lib/src/index.d.ts.map +1 -0
  31. package/lib/src/interfaces/types.d.ts +112 -0
  32. package/lib/src/interfaces/types.d.ts.map +1 -0
  33. package/lib/src/server.d.ts +16 -0
  34. package/lib/src/server.d.ts.map +1 -0
  35. package/lib/src/server.spec.d.ts +2 -0
  36. package/lib/src/server.spec.d.ts.map +1 -0
  37. package/lib/stub/command/index.command.d.ts +6 -0
  38. package/lib/stub/command/index.command.d.ts.map +1 -0
  39. package/lib/stub/command/index.spec.d.ts +2 -0
  40. package/lib/stub/command/index.spec.d.ts.map +1 -0
  41. package/lib/stub/middleware/configurable.middleware.d.ts +3 -0
  42. package/lib/stub/middleware/configurable.middleware.d.ts.map +1 -0
  43. package/lib/stub/middleware/configurable.spec.d.ts +2 -0
  44. package/lib/stub/middleware/configurable.spec.d.ts.map +1 -0
  45. package/lib/stub/middleware/new.middleware.d.ts +3 -0
  46. package/lib/stub/middleware/new.middleware.d.ts.map +1 -0
  47. package/lib/stub/middleware/new.spec.d.ts +2 -0
  48. package/lib/stub/middleware/new.spec.d.ts.map +1 -0
  49. package/package.json +4 -5
  50. package/lib/index.d.ts +0 -318
  51. package/lib/index.tsbuildinfo +0 -1
  52. package/src/app.spec.ts +0 -8
  53. package/src/app.ts +0 -13
  54. package/src/console/commands/make-command/index.command.ts +0 -52
  55. package/src/console/commands/make-command/index.spec.ts +0 -58
  56. package/src/console/commands/make-middleware/index.command.ts +0 -86
  57. package/src/console/commands/make-middleware/index.spec.ts +0 -73
  58. package/src/console/index.spec.ts +0 -14
  59. package/src/console/index.ts +0 -23
  60. package/src/database/connection.ts +0 -78
  61. package/src/database/mongodb/connection.ts +0 -339
  62. package/src/database/mongodb/mongodb-error-handler.ts +0 -63
  63. package/src/database/mongodb/mongodb-helper.ts +0 -109
  64. package/src/database/mongodb/mongodb-querystring.spec.ts +0 -80
  65. package/src/database/mongodb/mongodb-querystring.ts +0 -143
  66. package/src/index.ts +0 -42
  67. package/src/server.spec.ts +0 -68
  68. package/src/server.ts +0 -49
  69. package/src/types/controller.d.ts +0 -18
  70. package/src/types/database.d.ts +0 -104
  71. package/src/types/use-case.d.ts +0 -3
@@ -1,109 +0,0 @@
1
- import { isValid } from 'date-fns'
2
- import { ObjectId } from 'mongodb'
3
-
4
- /**
5
- * https://www.mongodb.com/docs/drivers/node/current/fundamentals/indexes/
6
- * https://www.mongodb.com/docs/manual/reference/collation/
7
- * https://www.mongodb.com/docs/manual/core/index-sparse/
8
- * https://www.mongodb.com/docs/manual/core/index-partial/
9
- */
10
- export class MongoDBHelper {
11
- private db
12
-
13
- constructor(db: IDatabase) {
14
- this.db = db
15
- }
16
-
17
- /**
18
- * Create unique column
19
- *
20
- * @example
21
- * create unique attribute "name"
22
- * createUnique(collection, {
23
- * name: -1,
24
- * })
25
- *
26
- * @example
27
- * create unique attribute for multiple column "first_name" and "last_name"
28
- * createUnique(collection, {
29
- * firstName: -1,
30
- * lastName: -1,
31
- * })
32
- */
33
- public async createUnique(collection: string, properties: object) {
34
- await this.db.createIndex(collection, properties, {
35
- unique: true,
36
- collation: {
37
- locale: 'en',
38
- strength: 2,
39
- },
40
- })
41
- }
42
-
43
- /**
44
- * Create unique if column is exists
45
- */
46
- public async createUniqueIfNotNull(collection: string, properties: object) {
47
- await this.db.createIndex(collection, properties, {
48
- unique: true,
49
- sparse: true,
50
- collation: {
51
- locale: 'en',
52
- strength: 2,
53
- },
54
- })
55
- }
56
-
57
- public async isExists(name: string) {
58
- const collections = (await this.db.listCollections()) as []
59
- return collections.some(function (collection: { name: string }) {
60
- return collection.name === name
61
- })
62
- }
63
-
64
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
65
- public static stringToObjectId(val: any): any {
66
- if (val == null) return null
67
- if (Array.isArray(val)) {
68
- return val.map((item) => {
69
- return MongoDBHelper.stringToObjectId(item)
70
- })
71
- } else if (typeof val === 'object' && !isValid(val)) {
72
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73
- return Object.keys(val).reduce((obj: any, key) => {
74
- const propVal = MongoDBHelper.stringToObjectId(val[key])
75
- obj[key] = propVal
76
- return obj
77
- }, {})
78
- } else if (typeof val === 'string' && ObjectId.isValid(val) && val === new ObjectId(val).toString()) {
79
- return new ObjectId(val)
80
- }
81
-
82
- return val
83
- }
84
-
85
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
- public static objectIdToString(val: any): any {
87
- if (val == null) return null
88
- if (Array.isArray(val)) {
89
- return val.map((item) => {
90
- return MongoDBHelper.objectIdToString(item)
91
- })
92
- } else if (typeof val === 'object' && ObjectId.isValid(val)) {
93
- return val.toString()
94
- } else if (typeof val === 'object' && !isValid(val)) {
95
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
- return Object.keys(val).reduce((obj: any, key) => {
97
- if (ObjectId.isValid(val) || isValid(val)) {
98
- return val.toString()
99
- } else {
100
- const propVal = MongoDBHelper.objectIdToString(val[key])
101
- obj[key] = propVal
102
- return obj
103
- }
104
- }, {})
105
- }
106
-
107
- return val
108
- }
109
- }
@@ -1,80 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
-
3
- import {
4
- convertArrayToObject,
5
- convertStringToArray,
6
- fields,
7
- filterExludeFields,
8
- limit,
9
- page,
10
- skip,
11
- sort,
12
- } from './mongodb-querystring'
13
-
14
- describe('field', () => {
15
- it('convert string to array', async () => {
16
- expect(convertStringToArray('name, password')).toStrictEqual(['name', 'password'])
17
- })
18
-
19
- it('convert array to mongodb field object', async () => {
20
- expect(convertArrayToObject(['name', 'password'])).toStrictEqual({
21
- name: 1,
22
- password: 1,
23
- })
24
- })
25
-
26
- it('add excluded fields to the object', async () => {
27
- const obj = { name: 1, password: 1 }
28
- const excluded = ['password']
29
- const result = {
30
- ...obj,
31
- ...filterExludeFields(obj, excluded),
32
- }
33
- expect(result).toStrictEqual({
34
- name: 1,
35
- password: 0,
36
- })
37
- })
38
-
39
- it('filter fields', async () => {
40
- const result = fields('', ['password'])
41
- expect(result).toStrictEqual({
42
- password: 0,
43
- })
44
- })
45
- })
46
-
47
- describe('page', () => {
48
- it('convert page string to number', async () => {
49
- expect(page('1')).toStrictEqual(1)
50
- })
51
- it('default page should be 1', async () => {
52
- expect(page()).toStrictEqual(1)
53
- })
54
- })
55
-
56
- describe('limit', () => {
57
- it('convert limit string to number', async () => {
58
- expect(limit('1')).toStrictEqual(1)
59
- })
60
- it('default limit should be 10', async () => {
61
- expect(limit()).toStrictEqual(10)
62
- })
63
- })
64
-
65
- describe('skip', () => {
66
- it('should skip number of data from page', async () => {
67
- expect(skip(1, 10)).toStrictEqual(0)
68
- expect(skip(2, 10)).toStrictEqual(10)
69
- expect(skip(2, 50)).toStrictEqual(50)
70
- })
71
- })
72
-
73
- describe('sort', () => {
74
- it('convert string to mongodb sort object', async () => {
75
- expect(sort('name,-address')).toStrictEqual({
76
- name: 1,
77
- address: -1,
78
- })
79
- })
80
- })
@@ -1,143 +0,0 @@
1
- import { SortDirection } from 'mongodb'
2
-
3
- interface IFieldsObject {
4
- [key: string]: number
5
- }
6
-
7
- interface ISortObject {
8
- [key: string]: SortDirection
9
- }
10
-
11
- /**
12
- * Parse query string to number
13
- *
14
- * @example
15
- * page("10") // => 10
16
- * page(10) // => 10
17
- */
18
- export function page(page: string | number = 1): number {
19
- if (typeof page === 'string') {
20
- return parseInt(page)
21
- }
22
-
23
- return page
24
- }
25
-
26
- /**
27
- * Parse query string to number
28
- *
29
- * @example
30
- * limit("10") // => 10
31
- * limit(10) // => 10
32
- */
33
- export function limit(pageSize: string | number = 10): number {
34
- if (typeof pageSize === 'string') {
35
- return parseInt(pageSize)
36
- }
37
-
38
- return pageSize
39
- }
40
-
41
- /**
42
- * Skip number of data from page
43
- *
44
- * @example
45
- * skip(1, 10) // => 0
46
- * skip(2, 10) // => 10
47
- * skip(3, 10) // => 20
48
- */
49
- export function skip(currentPage: string | number, pageSize: string | number): number {
50
- return (page(currentPage) - 1) * limit(pageSize)
51
- }
52
-
53
- export function filter(filter: never) {
54
- return filter
55
- }
56
-
57
- /**
58
- * Convert string param to mongodb field object
59
- *
60
- * @example
61
- * fields("name, address") // => { name: 1, address: 1 }
62
- */
63
- export function fields(fields = '', excludeFields: string[] = []): IFieldsObject {
64
- const obj: IFieldsObject = convertArrayToObject(convertStringToArray(fields))
65
-
66
- return filterExludeFields(obj, excludeFields)
67
- }
68
-
69
- /**
70
- * Convert string to array
71
- *
72
- * @example
73
- * convertStringToArray("name, address") // => ["name", "address"]
74
- */
75
- export function convertStringToArray(fields: string): string[] {
76
- return fields
77
- .split(' ')
78
- .join()
79
- .split(',')
80
- .filter((el) => el)
81
- }
82
-
83
- /**
84
- * Convert array to mongodb field object
85
- *
86
- * @example
87
- * convertArrayToObject(["name", "address"]) // => { name: 1, address: 1 }
88
- * convertArrayToObject(["name", "address"], -1) // => { name: -1, address: -1 }
89
- */
90
- export function convertArrayToObject(array: string[], value = 1): IFieldsObject {
91
- const obj: IFieldsObject = {}
92
- for (let i = 0; i < array.length; i++) {
93
- obj[`${array[i].trim()}`] = value
94
- }
95
- return obj
96
- }
97
-
98
- /**
99
- * Remove excluded fields
100
- *
101
- * @example
102
- * ex: { password: 0 }
103
- */
104
- export function filterExludeFields(obj: IFieldsObject, excludeFields: string[]): IFieldsObject {
105
- for (let i = 0; i < excludeFields.length; i++) {
106
- obj[`${excludeFields[i]}`] = 0
107
- }
108
- return obj
109
- }
110
-
111
- /**
112
- * Convert string param to mongodb sort object
113
- *
114
- * @example
115
- * sort("name, -createdAt") // => { name: 1, createdAt: -1 }
116
- */
117
- export function sort(fields: string): ISortObject {
118
- const obj: ISortObject = {}
119
-
120
- if (fields) {
121
- fields.split(',').forEach(function (field) {
122
- if (field.charAt(0) === '-') {
123
- field = field.substring(1)
124
- obj[field.trim()] = -1
125
- } else {
126
- obj[field.trim()] = 1
127
- }
128
- })
129
- }
130
- return obj
131
- }
132
-
133
- export default {
134
- page,
135
- limit,
136
- skip,
137
- sort,
138
- fields,
139
- filter,
140
- filterExludeFields,
141
- convertStringToArray,
142
- convertArrayToObject,
143
- }
package/src/index.ts DELETED
@@ -1,42 +0,0 @@
1
- import { BaseCommand } from '@point-hub/express-cli'
2
- import {
3
- ApiError,
4
- BaseError,
5
- errorHandlerMiddleware,
6
- find,
7
- invalidPathMiddleware,
8
- isTrustedError,
9
- } from '@point-hub/express-error-handler'
10
- import { MongoServerError } from 'mongodb'
11
-
12
- import { ConsoleKernel } from './console'
13
- import { DatabaseConnection } from './database/connection'
14
- import { MongoDBConnection } from './database/mongodb/connection'
15
- import { MongoErrorHandler } from './database/mongodb/mongodb-error-handler'
16
- import { MongoDBHelper } from './database/mongodb/mongodb-helper'
17
- import Querystring from './database/mongodb/mongodb-querystring'
18
-
19
- export const stubDir = import.meta.path.replace('/index.ts', '/../stub').replace('/index.js', '/../stub')
20
-
21
- // Console
22
- export { ExpressCli as BaseConsoleCli } from '@point-hub/express-cli'
23
- export const BaseConsoleCommand = BaseCommand
24
- export const BaseConsoleKernel = ConsoleKernel
25
- // Database
26
- export const BaseDatabaseConnection = DatabaseConnection
27
- export const BaseMongoDBConnection = MongoDBConnection
28
- export const BaseMongoDBHelper = MongoDBHelper
29
- export const BaseMongoDBQuerystring = Querystring
30
- export const BaseMongoServerError = MongoServerError
31
- export const BaseMongoErrorHandler = MongoErrorHandler
32
- // Error Handler
33
- export const BaseErrorHandler = {
34
- ApiError,
35
- BaseError,
36
- isTrustedError,
37
- getHttpError: find,
38
- errorHandlerMiddleware,
39
- invalidPathMiddleware,
40
- }
41
- // Server
42
- export { Server as BaseServer } from './server'
@@ -1,68 +0,0 @@
1
- import { describe, expect, it } from 'bun:test'
2
-
3
- import { createApp } from './app'
4
- import { BaseServer as Server } from './index'
5
-
6
- const port = 3001
7
-
8
- describe('server', () => {
9
- it('server should start on http://localhost:port', async () => {
10
- const server = new Server(await createApp())
11
-
12
- await server.start(port)
13
-
14
- expect(server.host).toEqual('localhost')
15
- expect(server.port).toEqual(port)
16
- expect(server.url).toEqual(`http://localhost:${port}`)
17
-
18
- server.stop()
19
- })
20
-
21
- it('get.url should not print out port if port = 80', async () => {
22
- const server = new Server(await createApp())
23
-
24
- await server.start(80)
25
-
26
- expect(server.url).toEqual(`http://localhost`)
27
-
28
- server.stop()
29
- })
30
-
31
- it('get.host return original ip', async () => {
32
- const server = new Server(await createApp())
33
- await server.start(port, '127.0.0.1')
34
- expect(server.host).toEqual('127.0.0.1')
35
- server.stop()
36
- })
37
-
38
- it('get.host return localhost', async () => {
39
- const server = new Server(await createApp())
40
- await server.start(port)
41
- expect(server.host).toEqual('localhost')
42
- server.stop()
43
- })
44
-
45
- it('not listening server should return undefined', async () => {
46
- const server = new Server(await createApp())
47
- server.stop()
48
- expect(server.host).toBeUndefined()
49
- expect(server.port).toBeUndefined()
50
- expect(server.url).toEqual('http://undefined:undefined')
51
- })
52
-
53
- it('using port that already in use return error', async () => {
54
- const server1 = new Server(await createApp())
55
- const server2 = new Server(await createApp())
56
-
57
- try {
58
- await server1.start(port)
59
-
60
- // start another server using same port to invoke error
61
- await server2.start(port)
62
- } catch (error) {
63
- expect(error).toBeDefined()
64
- server1.stop()
65
- server2.stop()
66
- }
67
- })
68
- })
package/src/server.ts DELETED
@@ -1,49 +0,0 @@
1
- import { Express } from 'express'
2
- import { Server as HttpServer } from 'http'
3
- import { AddressInfo } from 'net'
4
-
5
- export class Server {
6
- app: Express
7
- server: HttpServer | null = null
8
-
9
- constructor(app: Express) {
10
- this.app = app
11
- }
12
-
13
- listen(port: number, hostname?: string) {
14
- return new Promise((resolve, reject) => {
15
- if (hostname) {
16
- this.server = this.app.listen(port, hostname).once('listening', resolve).once('error', reject)
17
- } else {
18
- this.server = this.app.listen(port).once('listening', resolve).once('error', reject)
19
- }
20
- })
21
- }
22
-
23
- async start(port: number, hostname?: string) {
24
- await this.listen(port, hostname)
25
- }
26
-
27
- stop() {
28
- this.server?.close()
29
- this.server = null
30
- }
31
-
32
- get host() {
33
- const address = this.server?.address() as AddressInfo
34
- if (address?.address === '0.0.0.0' || address?.address === '::') {
35
- return 'localhost'
36
- } else {
37
- return address?.address
38
- }
39
- }
40
-
41
- get port() {
42
- const address = this.server?.address() as AddressInfo
43
- return address?.port
44
- }
45
-
46
- get url() {
47
- return `http://${this.host}${this.port !== 80 ? `:${this.port}` : ''}`
48
- }
49
- }
@@ -1,18 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- interface IHttpRequest {
3
- [key: string]: any
4
- }
5
-
6
- interface IControllerInput {
7
- httpRequest: IHttpRequest
8
- dbConnection: IDatabase
9
- }
10
-
11
- interface IController {
12
- (input: IControllerInput): Promise<IControllerOutput>
13
- }
14
-
15
- interface IControllerOutput {
16
- status: number
17
- json?: any
18
- }
@@ -1,104 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- interface IDocument {
3
- [key: string]: any
4
- }
5
-
6
- interface IQuery {
7
- fields?: string
8
- excludeFields?: string[]
9
- filter?: { [key: string]: unknown }
10
- page?: number
11
- pageSize?: number
12
- sort?: string
13
- }
14
-
15
- interface IPipeline {
16
- [key: string]: any
17
- }
18
-
19
- interface ICreateOutput {
20
- insertedId: string
21
- }
22
-
23
- interface ICreateManyOutput {
24
- insertedCount: number
25
- insertedIds: string[]
26
- }
27
-
28
- interface IRetrieveOutput {
29
- _id: string
30
- [key: string]: unknown
31
- }
32
-
33
- interface IRetrieveAllOutput {
34
- data: IRetrieveOutput[]
35
- pagination: {
36
- page: number
37
- pageCount: number
38
- pageSize: number
39
- totalDocument: number
40
- }
41
- }
42
-
43
- interface IUpdateOutput {
44
- matchedCount: number
45
- modifiedCount: number
46
- }
47
-
48
- interface IUpdateManyOutput {
49
- matchedCount: number
50
- modifiedCount: number
51
- }
52
-
53
- interface IDeleteOutput {
54
- deletedCount: number
55
- }
56
-
57
- interface IDeleteManyOutput {
58
- deletedCount: number
59
- }
60
-
61
- interface IAggregateOutput {
62
- data: IRetrieveOutput[]
63
- pagination: {
64
- page: number
65
- pageCount: number
66
- pageSize: number
67
- totalDocument: number
68
- }
69
- }
70
-
71
- interface IClientSession {
72
- startTransaction(options?: unknown): void
73
- commitTransaction(): Promise<void>
74
- abortTransaction(): Promise<void>
75
- endSession(): Promise<void>
76
- }
77
-
78
- interface IDatabase {
79
- session: unknown
80
- open(): Promise<void>
81
- close(): Promise<void>
82
- database(name: string, options?: unknown): this
83
- collection(name: string, options?: unknown): this
84
- listCollections(): Promise<{ name: string }[]>
85
- startSession(): IClientSession
86
- endSession(): Promise<void>
87
- startTransaction(): void
88
- commitTransaction(): Promise<void>
89
- abortTransaction(): Promise<void>
90
- createIndex(name: string, spec: unknown, options?: unknown): Promise<void>
91
- createCollection(name: string, options?: unknown): Promise<void>
92
- dropCollection(name: string, options?: unknown): Promise<void>
93
- updateSchema(name: string, schema: unknown): Promise<void>
94
- create(document: IDocument, options?: unknown): Promise<ICreateOutput>
95
- createMany(documents: IDocument[], options?: unknown): Promise<ICreateManyOutput>
96
- retrieveAll(query: IQuery, options?: unknown): Promise<IRetrieveAllOutput>
97
- retrieve(_id: string, options?: unknown): Promise<IRetrieveOutput>
98
- update(_id: string, document: IDocument, options?: unknown): Promise<IUpdateOutput>
99
- updateMany(filter: IDocument, document: IDocument, options?: unknown): Promise<IUpdateManyOutput>
100
- delete(_id: string, options?: unknown): Promise<IDeleteOutput>
101
- deleteMany(_ids: string[], options?: unknown): Promise<IDeleteManyOutput>
102
- deleteAll(options?: unknown): Promise<IDeleteManyOutput>
103
- aggregate(pipeline: IPipeline, query: IQuery, options?: unknown): Promise<IAggregateOutput>
104
- }
@@ -1,3 +0,0 @@
1
- interface IUseCase<TInput, TDeps, TOptions, TOutput> {
2
- handle(input: TInput, deps: TDeps, options?: TOptions): Promise<TOutput>
3
- }