File tree Expand file tree Collapse file tree 3 files changed +33
-23
lines changed Expand file tree Collapse file tree 3 files changed +33
-23
lines changed Original file line number Diff line number Diff line change 1- import { Schema , InferSchemaType } from 'mongoose' ;
2- import { SanitisedApiKey } from '../types/apiKey' ;
1+ import { Schema } from 'mongoose' ;
2+ import { SanitisedApiKey , ApiKeyDocument , ApiKeyModel } from '../types/apiKey' ;
33
4- export const apiKeySchema = new Schema (
4+ export const apiKeySchema = new Schema < ApiKeyDocument , ApiKeyModel > (
55 {
66 label : { type : String , default : 'API Key' } ,
77 lastUsedAt : { type : Date } ,
@@ -14,18 +14,12 @@ apiKeySchema.virtual('id').get(function getApiKeyId() {
1414 return this . _id . toHexString ( ) ;
1515} ) ;
1616
17- export interface ApiKeyVirtuals {
18- id : string ;
19- }
20-
21- export type ApiKeySchemaType = InferSchemaType < typeof apiKeySchema > ;
22-
2317/**
2418 * When serialising an APIKey instance, the `hashedKey` field
2519 * should never be exposed to the client. So we only return
2620 * a safe list of fields when toObject and toJSON are called.
2721 */
28- function apiKeyMetadata ( doc : any , _ret : any , _options : any ) : SanitisedApiKey {
22+ function apiKeyMetadata ( doc : ApiKeyDocument ) : SanitisedApiKey {
2923 return {
3024 id : doc . id ,
3125 label : doc . label ,
Original file line number Diff line number Diff line change 1- import { Model , model , Document } from 'mongoose' ;
2- import {
3- ApiKeySchemaType ,
4- apiKeySchema ,
5- ApiKeyVirtuals
6- } from '../models/apiKey' ;
1+ import { Model , Document , Types } from 'mongoose' ;
2+ import { VirtualId , MongooseTimestamps } from './mongoose' ;
73
8- export type ApiKeyDocument = Document & ApiKeySchemaType & ApiKeyVirtuals ;
4+ export interface IApiKey extends VirtualId , MongooseTimestamps {
5+ label : string ;
6+ lastUsedAt ?: Date ;
7+ hashedKey : string ;
8+ }
99
10- export type ApiKeyModel = Model < ApiKeyDocument > ;
11-
12- export const ApiKey = model < ApiKeyDocument , ApiKeyModel > (
13- 'ApiKey' ,
14- apiKeySchema
15- ) ;
10+ /** Mongoose document object for API Key */
11+ export interface ApiKeyDocument
12+ extends IApiKey ,
13+ Omit < Document < Types . ObjectId > , 'id' > {
14+ toJSON ( options ?: any ) : SanitisedApiKey ;
15+ toObject ( options ?: any ) : SanitisedApiKey ;
16+ }
1617
18+ /**
19+ * Sanitised API key object which hides the `hashedKey` field
20+ * and can be exposed to the client
21+ */
1722export interface SanitisedApiKey
1823 extends Pick < ApiKeyDocument , 'id' | 'label' | 'lastUsedAt' | 'createdAt' > { }
24+
25+ /** Mongoose model for API Key */
26+ export interface ApiKeyModel extends Model < ApiKeyDocument > { }
Original file line number Diff line number Diff line change 1+ export type VirtualId = {
2+ id : string ;
3+ } ;
4+
5+ export type MongooseTimestamps = {
6+ createdAt : Date ;
7+ updatedAt ?: Date ;
8+ } ;
You can’t perform that action at this time.
0 commit comments