feat: Add API key support
This commit is contained in:
79
src/models/APIKey/APIKeyDao.ts
Normal file
79
src/models/APIKey/APIKeyDao.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import { objectOmit } from '@dzeio/object-util'
|
||||
import mongoose from 'mongoose'
|
||||
import type APIKey from '.'
|
||||
import Client from '../Client'
|
||||
import Dao from '../Dao'
|
||||
|
||||
export default class APIKeyDao extends Dao<APIKey> {
|
||||
|
||||
// @ts-expect-error typing fix
|
||||
private model = mongoose.models['APIKey'] as null ?? mongoose.model('APIKey', new mongoose.Schema({
|
||||
user: { type: String, required: true },
|
||||
key: { type: String, required: true, unique: true, index: true},
|
||||
permissions: [{ type: String }]
|
||||
}, {
|
||||
timestamps: true
|
||||
}))
|
||||
|
||||
public async create(obj: Omit<APIKey, 'id' | 'created' | 'updated'>): Promise<APIKey | null> {
|
||||
await Client.get()
|
||||
return this.fromSource(await this.model.create(obj))
|
||||
}
|
||||
|
||||
public async findAll(query?: Partial<APIKey> | undefined): Promise<APIKey[]> {
|
||||
await Client.get()
|
||||
try {
|
||||
if (query?.id) {
|
||||
const item = await this.model.findById(new mongoose.Types.ObjectId(query.id))
|
||||
if (!item) {
|
||||
return []
|
||||
}
|
||||
return [this.fromSource(item)]
|
||||
}
|
||||
const resp = await this.model.find(query ? this.toSource(query as APIKey) : {})
|
||||
return resp.map(this.fromSource)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async update(obj: APIKey): Promise<APIKey | null> {
|
||||
await Client.get()
|
||||
|
||||
const query = await this.model.updateOne({
|
||||
_id: new mongoose.Types.ObjectId(obj.id)
|
||||
}, this.toSource(obj))
|
||||
if (query.matchedCount >= 1) {
|
||||
obj.updated = new Date()
|
||||
return obj
|
||||
}
|
||||
return null
|
||||
// return this.fromSource()
|
||||
}
|
||||
|
||||
public async delete(obj: APIKey): Promise<boolean> {
|
||||
await Client.get()
|
||||
const res = await this.model.deleteOne({
|
||||
_id: new mongoose.Types.ObjectId(obj.id)
|
||||
})
|
||||
return res.deletedCount > 0
|
||||
}
|
||||
|
||||
private toSource(obj: APIKey): Omit<APIKey, 'id'> {
|
||||
return objectOmit(obj, 'id', 'updated', 'created')
|
||||
}
|
||||
|
||||
private fromSource(doc: mongoose.Document<any, any, APIKey>): APIKey {
|
||||
return {
|
||||
id: doc._id.toString(),
|
||||
user: doc.get('user'),
|
||||
key: doc.get('key'),
|
||||
permissions: doc.get('permissions') ?? [],
|
||||
updated: doc.get('updatedAt'),
|
||||
created: doc.get('createdAt')
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
import type APIKey from '.'
|
||||
import Dao from '../Dao'
|
||||
|
||||
export default class APIKeyDao extends Dao<APIKey> {
|
||||
private idx = 0
|
||||
public async create(obj: Omit<APIKey, 'id'>): Promise<APIKey | null> {
|
||||
console.log('pouet', this.idx++)
|
||||
return null
|
||||
// throw new Error('Method not implemented.')
|
||||
}
|
||||
public async findAll(query?: Partial<APIKey> | undefined): Promise<APIKey[]> {
|
||||
throw new Error('Method not implemented.')
|
||||
}
|
||||
public async update(obj: APIKey): Promise<APIKey | null> {
|
||||
throw new Error('Method not implemented.')
|
||||
}
|
||||
public async delete(obj: APIKey): Promise<boolean> {
|
||||
throw new Error('Method not implemented.')
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
export default interface APIKey {
|
||||
id: string
|
||||
user: string
|
||||
key: string
|
||||
permissions: Array<string>
|
||||
created: Date
|
||||
updated: Date
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import Client from './Client'
|
||||
import APIKeyDao from './APIKey/APIKeyDao'
|
||||
import ConfigDao from './Config/ConfigDao'
|
||||
import Dao from './Dao'
|
||||
import UserDao from './User/UserDao'
|
||||
@ -17,6 +17,7 @@ import UserDao from './User/UserDao'
|
||||
interface DaoItem {
|
||||
config: ConfigDao
|
||||
user: UserDao
|
||||
apiKey: APIKeyDao
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,6 +58,7 @@ export default class DaoFactory {
|
||||
switch (item) {
|
||||
case 'config': return new ConfigDao()
|
||||
case 'user': return new UserDao()
|
||||
case 'apiKey': return new APIKeyDao()
|
||||
default: return undefined
|
||||
}
|
||||
}
|
||||
|
@ -6,19 +6,18 @@ import Dao from '../Dao'
|
||||
|
||||
export default class UserDao extends Dao<User> {
|
||||
|
||||
private model = mongoose.model('User', new mongoose.Schema({
|
||||
// @ts-expect-error typing fix
|
||||
private model = mongoose.models['User'] as null ?? mongoose.model('User', new mongoose.Schema({
|
||||
email: { type: String, required: true }
|
||||
}, {
|
||||
timestamps: true
|
||||
}))
|
||||
|
||||
private collection = 'users'
|
||||
|
||||
private idx = 0
|
||||
public async create(obj: Omit<User, 'id' | 'created' | 'updated'>): Promise<User | null> {
|
||||
await Client.get()
|
||||
return this.fromSource(await this.model.create(obj))
|
||||
}
|
||||
|
||||
public async findAll(query?: Partial<User> | undefined): Promise<User[]> {
|
||||
await Client.get()
|
||||
if (query?.id) {
|
||||
@ -32,6 +31,7 @@ export default class UserDao extends Dao<User> {
|
||||
return resp.map(this.fromSource)
|
||||
|
||||
}
|
||||
|
||||
public async update(obj: User): Promise<User | null> {
|
||||
await Client.get()
|
||||
|
||||
@ -45,6 +45,7 @@ export default class UserDao extends Dao<User> {
|
||||
return null
|
||||
// return this.fromSource()
|
||||
}
|
||||
|
||||
public async delete(obj: User): Promise<boolean> {
|
||||
await Client.get()
|
||||
const res = await this.model.deleteOne({
|
||||
|
Reference in New Issue
Block a user