Skip to content

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcuva committed Sep 6, 2023
1 parent 49861aa commit 2546e0d
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ interface SplitOptions {
grouping: GroupingObject;
}

interface GetUserParams {
byAPIKey: (apiKey: string) => unknown;
byEmail: (email: string) => unknown;
manualAPIKey?: string;
}

interface GetUserFunction {
(params: GetUserParams): unknown;
}

const splitIntoUserAndConfig = (inputObject: GroupingObject & Options): SplitOptions => {
const configKeys: (keyof Options)[] = [
'allowlist',
Expand All @@ -55,7 +65,7 @@ const splitIntoUserAndConfig = (inputObject: GroupingObject & Options): SplitOpt
return { grouping, config };
};

const guessWhereAPIKeyIs = (req: Request) => {
const guessWhereAPIKeyIs = (req: Request): string => {
// Authorization header
if (req.headers.authorization && req.headers.authorization.includes('Bearer')) {
return req.headers.authorization.split(' ')[1];
Expand All @@ -76,21 +86,21 @@ const guessWhereAPIKeyIs = (req: Request) => {
);

if (apiKeyHeader) {
return req.headers[apiKeyHeader];
return req.headers[apiKeyHeader] as string;
}

// Is it a cookie?
// Ok idk what to do for this case yet

// Is it a query param?
if (req.query.api_key) {
return req.query.api_key;
return req.query.api_key as string;
} else if (req.query.apiKey) {
return req.query.apiKey;
return req.query.apiKey as string;
}

// error case where we tell them to go the manual route
return null;
throw new Error('test');
};

// See comment at the auth definition below
Expand All @@ -99,14 +109,14 @@ let readmeAPIKey = '';
let readmeProjectData: GetProjectResponse200 | undefined;

const readme = (
userFunction: (req: Request, getUser) => GroupingObject & Options,
userFunction: (req: Request, getUser: GetUserFunction) => GroupingObject & Options,
{ disableWebhook, disableMetrics } = {
disableWebhook: false,
disableMetrics: false,
}
) => {
return async (req: Request, res: Response, next: NextFunction) => {
const getUser = ({ byAPIKey, byEmail, manualAPIKey }) => {
const getUser = ({ byAPIKey, byEmail, manualAPIKey }: GetUserParams): unknown => {
if (req.path === '/readme-webhook' && req.method === 'POST' && !disableWebhook) {
const user = byEmail(req.body.email);
if (!user) {
Expand All @@ -121,11 +131,10 @@ const readme = (
return byAPIKey(manualAPIKey);
}
// Try to figure out where the api key is
requestAPIKey = guessWhereAPIKeyIs(req);
if (!requestAPIKey) {
console.error(
'Unable to find API key automatically. Please use the manual method by passing the api key in via `manualAPIKey` to getUser.'
);
try {
requestAPIKey = guessWhereAPIKeyIs(req);
} catch (e) {
console.error(e);
}
return byAPIKey(requestAPIKey);
};
Expand Down Expand Up @@ -189,6 +198,11 @@ const readme = (
const { grouping, config } = splitIntoUserAndConfig(user);
const filteredKey = grouping.keys.find(key => key.apiKey === requestAPIKey);

if (!filteredKey || !filteredKey.apiKey) {
console.error(`API key ${requestAPIKey} not found`);
return next();
}

log(readmeAPIKey, req, res, { apiKey: filteredKey.apiKey, label: filteredKey.name, email: grouping.email }, config);
return next();
};
Expand Down

0 comments on commit 2546e0d

Please sign in to comment.