Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: the router is not a target, refactor out of the targets dir #125

Merged
merged 3 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion integration-tests/aws-sdk/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { StormDBDataStoreFactory } from "../../src/services/dataStore/stormDb";
import { otp } from "../../src/services/otp";
import { JwtTokenGenerator } from "../../src/services/tokenGenerator";
import { UserPoolServiceFactoryImpl } from "../../src/services/userPoolService";
import { Router } from "../../src/targets/router";
import { Router } from "../../src/server/Router";

const mkdtemp = promisify(fs.mkdtemp);
const rmdir = promisify(fs.rmdir);
Expand Down
3 changes: 2 additions & 1 deletion src/targets/router.test.ts → src/server/Router.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TestContext } from "../__tests__/testContext";
import { UnsupportedError } from "../errors";
import { Services } from "../services";
import { Router, Targets } from "./router";
import { Router } from "./Router";
import { Targets } from "../targets/Target";

describe("Router", () => {
it("returns an error handler for an invalid target", async () => {
Expand Down
38 changes: 38 additions & 0 deletions src/server/Router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Services } from "../services";
import { UnsupportedError } from "../errors";
import { Targets, isSupportedTarget } from "../targets/Target";
import { Context } from "../services/context";

// eslint-disable-next-line
export type Route = (ctx: Context, req: any) => Promise<any>;
export type Router = (target: string) => Route;

export const Router =
(services: Services): Router =>
(target: string) => {
if (!isSupportedTarget(target)) {
return () =>
Promise.reject(
new UnsupportedError(`Unsupported x-amz-target header "${target}"`)
);
}

const t = Targets[target](services);

return async (ctx, req) => {
const targetLogger = ctx.logger.child({
target,
});

targetLogger.debug("start");
const res = await t(
{
...ctx,
logger: targetLogger,
},
req
);
targetLogger.debug("end");
return res;
};
};
2 changes: 1 addition & 1 deletion src/server/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { MessageDeliveryService } from "../services/messageDelivery/messageDeliv
import { otp } from "../services/otp";
import { JwtTokenGenerator } from "../services/tokenGenerator";
import { UserPoolServiceFactoryImpl } from "../services/userPoolService";
import { Router } from "../targets/router";
import { Router } from "./Router";
import { loadConfig } from "./config";
import { createServer, Server } from "./server";

Expand Down
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as http from "http";
import type { Logger } from "pino";
import * as uuid from "uuid";
import { CognitoError, UnsupportedError } from "../errors";
import { Router } from "../targets/router";
import { Router } from "./Router";
import PublicKey from "../keys/cognitoLocal.public.json";
import Pino from "pino-http";

Expand Down
1 change: 0 additions & 1 deletion src/services/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { LogService } from "./LogService";

export interface Context {
readonly logger: LogService;
}
43 changes: 4 additions & 39 deletions src/targets/router.ts → src/targets/Target.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LogService } from "../services/LogService";
import { Services } from "../services";
import { UnsupportedError } from "../errors";
import { Context } from "../services/context";

import { AdminDeleteUserAttributes } from "./adminDeleteUserAttributes";
import { AdminSetUserPassword } from "./adminSetUserPassword";
import { ConfirmForgotPassword } from "./confirmForgotPassword";
Expand Down Expand Up @@ -63,47 +62,13 @@ export const Targets = {
VerifyUserAttribute,
} as const;

type TargetName = keyof typeof Targets;
export type TargetName = keyof typeof Targets;

export type Context = { readonly logger: LogService };
export type Target<Req extends {}, Res extends {}> = (
ctx: Context,
req: Req
) => Promise<Res>;

//export const registerTarget = (target: Target) => Targets
export const isSupportedTarget = (name: string): name is TargetName =>
Object.keys(Targets).includes(name);

// eslint-disable-next-line
export type Route = (ctx: Context, req: any) => Promise<any>;
export type Router = (target: string) => Route;

export const Router =
(services: Services): Router =>
(target: string) => {
if (!isSupportedTarget(target)) {
return () =>
Promise.reject(
new UnsupportedError(`Unsupported x-amz-target header "${target}"`)
);
}

const t = Targets[target](services);

return async (ctx, req) => {
const targetLogger = ctx.logger.child({
target,
});

targetLogger.debug("start");
const res = await t(
{
...ctx,
logger: targetLogger,
},
req
);
targetLogger.debug("end");
return res;
};
};
2 changes: 1 addition & 1 deletion src/targets/adminConfirmSignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { Services } from "../services";
import { NotAuthorizedError } from "../errors";
import { attribute, attributesAppend } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type AdminConfirmSignUpTarget = Target<
AdminConfirmSignUpRequest,
Expand Down
3 changes: 2 additions & 1 deletion src/targets/adminCreateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
attributeValue,
User,
} from "../services/userPoolService";
import { Context, Target } from "./router";
import { Context } from "../services/context";
import { Target } from "./Target";

const generator = shortUUID(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"
Expand Down
2 changes: 1 addition & 1 deletion src/targets/adminDeleteUser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AdminDeleteUserRequest } from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { UserNotFoundError } from "../errors";
import { Target } from "./router";
import { Target } from "./Target";

export type AdminDeleteUserTarget = Target<AdminDeleteUserRequest, {}>;

Expand Down
2 changes: 1 addition & 1 deletion src/targets/adminDeleteUserAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { NotAuthorizedError } from "../errors";
import { Services } from "../services";
import { attributesRemove } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type AdminDeleteUserAttributesTarget = Target<
AdminDeleteUserAttributesRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/adminGetUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { UserNotFoundError } from "../errors";
import { Target } from "./router";
import { Target } from "./Target";

export type AdminGetUserTarget = Target<
AdminGetUserRequest,
Expand Down
3 changes: 2 additions & 1 deletion src/targets/adminInitiateAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
UnsupportedError,
} from "../errors";
import { Services } from "../services";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

export type AdminInitiateAuthTarget = Target<
AdminInitiateAuthRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/adminSetUserPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { UserNotFoundError } from "../errors";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type AdminSetUserPasswordTarget = Target<
AdminSetUserPasswordRequest,
Expand Down
3 changes: 2 additions & 1 deletion src/targets/adminUpdateUserAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
User,
validatePermittedAttributeChanges,
} from "../services/userPoolService";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

const sendAttributeVerificationCode = async (
ctx: Context,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/changePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
NotAuthorizedError,
} from "../errors";
import { Token } from "../services/tokenGenerator";
import { Target } from "./router";
import { Target } from "./Target";

export type ChangePasswordTarget = Target<
ChangePasswordRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/confirmForgotPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { CodeMismatchError, UserNotFoundError } from "../errors";
import { Services } from "../services";
import { attribute, attributesAppend } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type ConfirmForgotPasswordTarget = Target<
ConfirmForgotPasswordRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/confirmSignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { CodeMismatchError, NotAuthorizedError } from "../errors";
import { Services } from "../services";
import { attribute, attributesAppend } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type ConfirmSignUpTarget = Target<
ConfirmSignUpRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/createGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { Group } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type CreateGroupTarget = Target<CreateGroupRequest, CreateGroupResponse>;

Expand Down
2 changes: 1 addition & 1 deletion src/targets/createUserPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import shortUUID from "short-uuid";
import { Services } from "../services";
import { USER_POOL_AWS_DEFAULTS } from "../services/cognitoService";
import { Target } from "./router";
import { Target } from "./Target";

const REGION = "local";
const ACCOUNT_ID = "local";
Expand Down
2 changes: 1 addition & 1 deletion src/targets/createUserPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
CreateUserPoolClientResponse,
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type CreateUserPoolClientTarget = Target<
CreateUserPoolClientRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/deleteUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import jwt from "jsonwebtoken";
import { InvalidParameterError, NotAuthorizedError } from "../errors";
import { Services } from "../services";
import { Token } from "../services/tokenGenerator";
import { Target } from "./router";
import { Target } from "./Target";

export type DeleteUserTarget = Target<DeleteUserRequest, {}>;

Expand Down
2 changes: 1 addition & 1 deletion src/targets/deleteUserAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { InvalidParameterError, NotAuthorizedError } from "../errors";
import { Services } from "../services";
import { Token } from "../services/tokenGenerator";
import { attributesRemove } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type DeleteUserAttributesTarget = Target<
DeleteUserAttributesRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/describeUserPoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { ResourceNotFoundError } from "../errors";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type DescribeUserPoolClientTarget = Target<
DescribeUserPoolClientRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/forgotPassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { UnsupportedError, UserNotFoundError } from "../errors";
import { Services } from "../services";
import { DeliveryDetails } from "../services/messageDelivery/messageDelivery";
import { attributeValue } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type ForgotPasswordTarget = Target<
ForgotPasswordRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jwt from "jsonwebtoken";
import { InvalidParameterError, UserNotFoundError } from "../errors";
import { Services } from "../services";
import { Token } from "../services/tokenGenerator";
import { Target } from "./router";
import { Target } from "./Target";

export type GetUserTarget = Target<GetUserRequest, GetUserResponse>;

Expand Down
3 changes: 2 additions & 1 deletion src/targets/getUserAttributeVerificationCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { InvalidParameterError, UserNotFoundError } from "../errors";
import { selectAppropriateDeliveryMethod } from "../services/messageDelivery/deliveryMethod";
import { Token } from "../services/tokenGenerator";
import { User } from "../services/userPoolService";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

const sendAttributeVerificationCode = async (
ctx: Context,
Expand Down
3 changes: 2 additions & 1 deletion src/targets/initiateAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
MFAOption,
User,
} from "../services/userPoolService";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

export type InitiateAuthTarget = Target<
InitiateAuthRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/listGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ListGroupsResponse,
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type ListGroupsTarget = Target<ListGroupsRequest, ListGroupsResponse>;

Expand Down
2 changes: 1 addition & 1 deletion src/targets/listUserPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { UserPool } from "../services/userPoolService";
import { Target } from "./router";
import { Target } from "./Target";

export type ListUserPoolsTarget = Target<
ListUserPoolsRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/listUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ListUsersResponse,
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type ListUsersTarget = Target<ListUsersRequest, ListUsersResponse>;

Expand Down
2 changes: 1 addition & 1 deletion src/targets/respondToAuthChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
UnsupportedError,
} from "../errors";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type RespondToAuthChallengeTarget = Target<
RespondToAuthChallengeRequest,
Expand Down
2 changes: 1 addition & 1 deletion src/targets/revokeToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from "aws-sdk/clients/cognitoidentityserviceprovider";
import { NotAuthorizedError } from "../errors";
import { Services } from "../services";
import { Target } from "./router";
import { Target } from "./Target";

export type RevokeTokenTarget = Target<RevokeTokenRequest, RevokeTokenResponse>;

Expand Down
3 changes: 2 additions & 1 deletion src/targets/signUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
attributeValue,
User,
} from "../services/userPoolService";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

export type SignUpTarget = Target<SignUpRequest, SignUpResponse>;

Expand Down
3 changes: 2 additions & 1 deletion src/targets/updateUserAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
User,
validatePermittedAttributeChanges,
} from "../services/userPoolService";
import { Context, Target } from "./router";
import { Target } from "./Target";
import { Context } from "../services/context";

const sendAttributeVerificationCode = async (
ctx: Context,
Expand Down
Loading