Added Metadata to APIKey API routes

This commit is contained in:
Jyotirmoy Bandyopadhayaya 2023-06-24 13:08:48 +05:30
parent 9525eb5202
commit 3369a826c5
Signed by: bravo68web
GPG Key ID: F5671FD7BCB9917A
3 changed files with 34 additions and 10 deletions

View File

@ -15,13 +15,16 @@ export default class APIKeyController
next: NextFunction
): Promise<Response | void> => {
let apikeys;
let metadata;
try {
const { masterkey } = req.query as { masterkey: string };
apikeys = await this.listS(masterkey);
const { data, meta } = await this.listS(masterkey);
apikeys = data;
metadata = meta;
} catch (error) {
return next(error);
}
return res.status(200).json(makeResponse(apikeys));
return res.status(200).json(makeResponse(apikeys, metadata));
};
public generate = async (

View File

@ -1,7 +1,7 @@
import { NextFunction, Response } from 'express';
import { ModRequest } from '../types';
import { Apikeys } from '../graphql/types';
import { encapDataKey } from '../libs'
import { encapDataKey } from '../libs';
export interface IAPIKeyController {
list(
@ -27,13 +27,20 @@ export interface IAPIKeyController {
}
export interface IAPIKeyService {
listS(masterkey: string): Promise<encapDataKey[]>;
listS(masterkey: string): Promise<IListAPIKeys>;
generateS(masterkey: string): Promise<Apikeys>;
deleteS(apikey: string, masterkey: string): Promise<number>;
checkS(apikey: string): Promise<Apikeys | null>;
verifyS(apikey: string): Promise<boolean>;
}
export interface IListAPIKeys {
data: encapDataKey[];
meta: {
total: number;
};
}
export interface IAPIKeyAuth {
check(req: ModRequest, res: Response, next: NextFunction): Promise<void>;
}

View File

@ -1,9 +1,8 @@
import { gql } from 'graphql-request';
import { client } from '../helpers';
import { IAPIKeyService } from '../interfaces/apikey.interface';
import { encapDataKeys } from '../libs';
import { IAPIKeyService, IListAPIKeys } from '../interfaces/apikey.interface';
import { encapDataKeys, encapDataKey } from '../libs';
import { Apikeys, Apikeys_Mutation_Response } from '../graphql/types';
import { encapDataKey } from '../libs';
export default class APIKeyService implements IAPIKeyService {
public async checkS(apikey: string): Promise<Apikeys | null> {
const query = gql`
@ -25,7 +24,6 @@ export default class APIKeyService implements IAPIKeyService {
if (result.apikeys.length === 0) {
return null;
} else {
// return result.apikeys[0];
const updateLastUsedquery = gql`
mutation updateLastUsed($apikeyID: uuid!) {
update_apikeys_by_pk(
@ -84,7 +82,7 @@ export default class APIKeyService implements IAPIKeyService {
return result.delete_apikeys.affected_rows;
}
public async listS(masterKey: string): Promise<encapDataKey[]> {
public async listS(masterKey: string): Promise<IListAPIKeys> {
if (masterKey !== process.env.MASTER_KEY)
throw new Error('Invalid master key');
const query = gql`
@ -94,12 +92,28 @@ export default class APIKeyService implements IAPIKeyService {
keyID
last_used
}
apikeys_aggregate {
aggregate {
count
}
}
}
`;
const result: {
apikeys: Apikeys[];
apikeys_aggregate: {
aggregate: {
count: number;
};
};
} = await client.request(query);
return encapDataKeys(result.apikeys);
return {
data: encapDataKeys(result.apikeys),
meta: {
total: result.apikeys_aggregate.aggregate.count,
},
};
}
public async verifyS(apikey: string): Promise<boolean> {