Skip to content

Commit

Permalink
♻️ refactor: use default formatting rules for biome
Browse files Browse the repository at this point in the history
  • Loading branch information
larwaa committed Dec 21, 2023
1 parent 7e35534 commit 31cdfbd
Show file tree
Hide file tree
Showing 194 changed files with 19,614 additions and 18,177 deletions.
5 changes: 0 additions & 5 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
"recommended": true
}
},
"formatter": {
"indentWidth": 2,
"indentStyle": "space",
"lineWidth": 120
},
"files": {
"ignore": [
"node_modules",
Expand Down
88 changes: 52 additions & 36 deletions src/__tests__/dependencies-factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { PrismaClient } from "@prisma/client";
import { MockOpenIdClient, newMockOpenIdClient } from "~/__tests__/mocks/openIdClient.js";
import {
MockOpenIdClient,
newMockOpenIdClient,
} from "~/__tests__/mocks/openIdClient.js";
import { ApolloServerDependencies } from "~/lib/apollo-server.js";
import { ServerDependencies, dependenciesFactory } from "~/lib/fastify/dependencies.js";
import {
ServerDependencies,
dependenciesFactory,
} from "~/lib/fastify/dependencies.js";
import { MemberRepository } from "~/repositories/organizations/members.js";
import { OrganizationRepository } from "~/repositories/organizations/organizations.js";
import { UserRepository } from "~/repositories/users/index.js";
Expand All @@ -10,44 +16,54 @@ import { PermissionService } from "~/services/permissions/service.js";
import { UserService } from "~/services/users/service.js";

export function defaultTestDependenciesFactory(
overrides: Partial<{
apolloServerDependencies: Partial<ApolloServerDependencies>;
authService: AuthService;
prismaClient: PrismaClient;
openIdClient: MockOpenIdClient;
}> = {},
overrides: Partial<{
apolloServerDependencies: Partial<ApolloServerDependencies>;
authService: AuthService;
prismaClient: PrismaClient;
openIdClient: MockOpenIdClient;
}> = {},
): ServerDependencies & { mockOpenIdClient: MockOpenIdClient } {
const defaultDependencies = dependenciesFactory();
const { prismaClient: prismaOverride, apolloServerDependencies: apolloServerOverrides } = overrides;
const prismaClient = prismaOverride ?? defaultDependencies.prisma;
const defaultDependencies = dependenciesFactory();
const {
prismaClient: prismaOverride,
apolloServerDependencies: apolloServerOverrides,
} = overrides;
const prismaClient = prismaOverride ?? defaultDependencies.prisma;

const { openIdClient = newMockOpenIdClient() } = overrides;
const { apolloServerDependencies: serviceOverrides = {} } = overrides;
const { openIdClient = newMockOpenIdClient() } = overrides;
const { apolloServerDependencies: serviceOverrides = {} } = overrides;

const memberRepository = new MemberRepository(prismaClient);
const organizationRepository = new OrganizationRepository(prismaClient);
const userRepository = new UserRepository(prismaClient);
const { permissionService = new PermissionService(memberRepository, userRepository, organizationRepository) } =
serviceOverrides;
const { userService = new UserService(userRepository, permissionService) } = serviceOverrides;
const { authService = new AuthService(userService, openIdClient) } = overrides;
const memberRepository = new MemberRepository(prismaClient);
const organizationRepository = new OrganizationRepository(prismaClient);
const userRepository = new UserRepository(prismaClient);
const {
permissionService = new PermissionService(
memberRepository,
userRepository,
organizationRepository,
),
} = serviceOverrides;
const { userService = new UserService(userRepository, permissionService) } =
serviceOverrides;
const { authService = new AuthService(userService, openIdClient) } =
overrides;

const defaultApolloServerOverrides: Partial<ApolloServerDependencies> = {
userService,
permissionService,
};
const defaultApolloServerOverrides: Partial<ApolloServerDependencies> = {
userService,
permissionService,
};

const apolloServerDependencies: ApolloServerDependencies = {
...defaultDependencies.apolloServerDependencies,
...defaultApolloServerOverrides,
...apolloServerOverrides,
};
const apolloServerDependencies: ApolloServerDependencies = {
...defaultDependencies.apolloServerDependencies,
...defaultApolloServerOverrides,
...apolloServerOverrides,
};

return {
...defaultDependencies,
authService,
mockOpenIdClient: openIdClient,
apolloServerDependencies,
prisma: prismaClient,
};
return {
...defaultDependencies,
authService,
mockOpenIdClient: openIdClient,
apolloServerDependencies,
prisma: prismaClient,
};
}
26 changes: 13 additions & 13 deletions src/__tests__/integration/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { initServer } from "../../server.js";
import { defaultTestDependenciesFactory } from "../dependencies-factory.js";

describe("Server", () => {
let server: Awaited<ReturnType<typeof initServer>> | undefined;
let server: Awaited<ReturnType<typeof initServer>> | undefined;

beforeAll(async () => {
const dependencies = defaultTestDependenciesFactory();
server = await initServer(dependencies, { port: 4003, host: "0.0.0.0" });
});
beforeAll(async () => {
const dependencies = defaultTestDependenciesFactory();
server = await initServer(dependencies, { port: 4003, host: "0.0.0.0" });
});

test("Get data from server", async () => {
await server?.inject({ method: "GET", url: "/-/health" }).then((res) => {
expect(res.statusCode).toBe(200);
});
});
test("Get data from server", async () => {
await server?.inject({ method: "GET", url: "/-/health" }).then((res) => {
expect(res.statusCode).toBe(200);
});
});

afterAll(() => {
server?.close();
});
afterAll(() => {
server?.close();
});
});
75 changes: 39 additions & 36 deletions src/__tests__/mocks/openIdClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,50 @@ import { OpenIDClient } from "../../services/auth/service.js";

type UserResponse = Pick<User, "id" | "email"> & { name: string };
export type MockOpenIdClient = DeepMockProxy<OpenIDClient> & {
updateUserResponseMock: (user: UserResponse) => void;
updateUserResponseMock: (user: UserResponse) => void;
};

export function newMockOpenIdClient(): MockOpenIdClient {
const mockClient = mockDeep<OpenIDClient>();
mockClient.authorizationUrl.mockReturnValue("https://example.com");
const mockClient = mockDeep<OpenIDClient>();
mockClient.authorizationUrl.mockReturnValue("https://example.com");

const mockClientWithUpdateUserResponseMock = merge(mockClient, {
updateUserResponseMock(user: UserResponse) {
updateUserResponseMock(mockClient, user);
},
});
mockClientWithUpdateUserResponseMock.updateUserResponseMock({
id: faker.string.uuid(),
email: faker.internet.exampleEmail({ firstName: faker.string.uuid() }),
name: faker.person.fullName(),
});
const mockClientWithUpdateUserResponseMock = merge(mockClient, {
updateUserResponseMock(user: UserResponse) {
updateUserResponseMock(mockClient, user);
},
});
mockClientWithUpdateUserResponseMock.updateUserResponseMock({
id: faker.string.uuid(),
email: faker.internet.exampleEmail({ firstName: faker.string.uuid() }),
name: faker.person.fullName(),
});

return mockClientWithUpdateUserResponseMock;
return mockClientWithUpdateUserResponseMock;
}

function updateUserResponseMock(mockOpenIdClient: DeepMockProxy<OpenIDClient>, user: UserResponse) {
mockOpenIdClient.callback.mockResolvedValue({
id_token: "id_token",
access_token: "access_token",
expired: () => false,
claims() {
return mock<IdTokenClaims>({
sub: user.id,
email: user.email,
name: user.name,
"https://n.feide.no/claims/userid_sec": [`feide:${user.email}`],
"https://n.feide.no/claims/eduPersonPrincipalName": user.email,
});
},
});
mockOpenIdClient.userinfo.mockResolvedValue({
sub: user.id,
email: user.email,
name: user.name,
"https://n.feide.no/claims/userid_sec": [`feide:${user.email}`],
"https://n.feide.no/claims/eduPersonPrincipalName": user.email,
});
function updateUserResponseMock(
mockOpenIdClient: DeepMockProxy<OpenIDClient>,
user: UserResponse,
) {
mockOpenIdClient.callback.mockResolvedValue({
id_token: "id_token",
access_token: "access_token",
expired: () => false,
claims() {
return mock<IdTokenClaims>({
sub: user.id,
email: user.email,
name: user.name,
"https://n.feide.no/claims/userid_sec": [`feide:${user.email}`],
"https://n.feide.no/claims/eduPersonPrincipalName": user.email,
});
},
});
mockOpenIdClient.userinfo.mockResolvedValue({
sub: user.id,
email: user.email,
name: user.name,
"https://n.feide.no/claims/userid_sec": [`feide:${user.email}`],
"https://n.feide.no/claims/eduPersonPrincipalName": user.email,
});
}
68 changes: 34 additions & 34 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,40 @@ dotenv.config({ path: ".env" });
dotenv.config();

const envVarsSchema = z.object({
CORS_CREDENTIALS: z.string().transform((v) => v === "true"),
CORS_ORIGINS: z.string().transform((v) => v.split(",")),
DATABASE_CONNECTION_STRING: z.string(),
FEIDE_BASE_URL: z.string().url(),
FEIDE_CLIENT_ID: z.string(),
FEIDE_CLIENT_SECRET: z.string(),
LOG_ENABLED: z
.string()
.default("false")
.transform((v) => v === "true"),
NO_REPLY_EMAIL: z.string().email(),
NODE_ENV: z.enum(["development", "production", "test"]),
PORT: z
.string()
.transform((val) => Number.parseInt(val))
.default("4000"),
POSTMARK_API_TOKEN: z.string(),
RATE_LIMIT_MAX: z
.string()
.transform((val) => Number.parseInt(val))
.default("1000"),
REDIS_CONNECTION_STRING: z.string(),
SENTRY_DSN: z.string().optional(),
SENTRY_RELEASE: z.string().optional(),
SENTRY_TRACES_SAMPLE_RATE: z
.string()
.transform((val) => Number.parseFloat(val))
.default("1"),
SERVER_URL: z.string(),
SESSION_COOKIE_DOMAIN: z.string(),
SESSION_COOKIE_HTTP_ONLY: z.string().transform((val) => val === "true"),
SESSION_COOKIE_NAME: z.string(),
SESSION_COOKIE_SECURE: z.string().transform((val) => val === "true"),
SESSION_SECRET: z.string(),
CORS_CREDENTIALS: z.string().transform((v) => v === "true"),
CORS_ORIGINS: z.string().transform((v) => v.split(",")),
DATABASE_CONNECTION_STRING: z.string(),
FEIDE_BASE_URL: z.string().url(),
FEIDE_CLIENT_ID: z.string(),
FEIDE_CLIENT_SECRET: z.string(),
LOG_ENABLED: z
.string()
.default("false")
.transform((v) => v === "true"),
NO_REPLY_EMAIL: z.string().email(),
NODE_ENV: z.enum(["development", "production", "test"]),
PORT: z
.string()
.transform((val) => Number.parseInt(val))
.default("4000"),
POSTMARK_API_TOKEN: z.string(),
RATE_LIMIT_MAX: z
.string()
.transform((val) => Number.parseInt(val))
.default("1000"),
REDIS_CONNECTION_STRING: z.string(),
SENTRY_DSN: z.string().optional(),
SENTRY_RELEASE: z.string().optional(),
SENTRY_TRACES_SAMPLE_RATE: z
.string()
.transform((val) => Number.parseFloat(val))
.default("1"),
SERVER_URL: z.string(),
SESSION_COOKIE_DOMAIN: z.string(),
SESSION_COOKIE_HTTP_ONLY: z.string().transform((val) => val === "true"),
SESSION_COOKIE_NAME: z.string(),
SESSION_COOKIE_SECURE: z.string().transform((val) => val === "true"),
SESSION_SECRET: z.string(),
});

export const env = envVarsSchema.parse(process.env);
8 changes: 4 additions & 4 deletions src/domain/cabins.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const BookingStatus = {
PENDING: "PENDING",
CONFIRMED: "CONFIRMED",
CANCELLED: "CANCELLED",
REJECTED: "REJECTED",
PENDING: "PENDING",
CONFIRMED: "CONFIRMED",
CANCELLED: "CANCELLED",
REJECTED: "REJECTED",
} as const;

export type BookingStatus = (typeof BookingStatus)[keyof typeof BookingStatus];
Loading

0 comments on commit 31cdfbd

Please sign in to comment.