Skip to content

Commit

Permalink
TES-306: Create test server for API routes (#298)
Browse files Browse the repository at this point in the history
* Add mock auth context

* Improve API endpoints

* Add more meta routes

* Change to use headers

* Fix create type check error for collection

* Add context inner type
  • Loading branch information
HansGabriel authored Dec 17, 2023
1 parent 2e2552b commit 8161827
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 11 deletions.
5 changes: 4 additions & 1 deletion apps/expo/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { ExpoConfig, ConfigContext } from "@expo/config";

type ENV = "development" | "production";

const CLERK_PUBLISHABLE_KEY =
"pk_test_Z3Jvd2luZy1kb2Jlcm1hbi04OC5jbGVyay5hY2NvdW50cy5kZXYk";
const SERVER_URL = "https://test-trek-prod.vercel.app";
const SERVER_ENV: "development" | "production" = "development";
const SERVER_ENV: ENV = "development";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const defineConfig = (_ctx: ConfigContext): ExpoConfig => ({
name: "TestTrek",
slug: "testtrek",
Expand Down
1 change: 0 additions & 1 deletion apps/expo/src/components/search/SearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export const SearchField: FC<FieldProps> = ({
containerStyle={{
backgroundColor: "transparent",
borderBottomColor: "transparent",
borderTopColor: "transparent",
borderLeftColor: "transparent",
borderRightColor: "transparent",
width: "80%", // Adjusted width
Expand Down
12 changes: 12 additions & 0 deletions packages/api/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { type inferAsyncReturnType } from "@trpc/server";
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
import { getAuth } from "@clerk/nextjs/server";

const isTest = process.env.SERVER_ENV === "test";

/**
* Replace this with an object if you want to pass things to createContextInner
*/
Expand All @@ -22,11 +24,21 @@ export const createContextInner = async ({ auth }: AuthContextProps) => {
};
};

type ContextInner = inferAsyncReturnType<typeof createContextInner>;

/**
* This is the actual context you'll use in your router
* @link https://trpc.io/docs/context
**/
export const createContext = async (opts: CreateNextContextOptions) => {
if (isTest) {
return {
auth: {
userId: opts.req.headers["authorization"],
},
prisma,
} as ContextInner;
}
return await createContextInner({ auth: getAuth(opts.req) });
};

Expand Down
12 changes: 6 additions & 6 deletions packages/api/src/router/algoliaSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {

export const algoliaSearch = router({
algoliaSearch: protectedProcedure
// .meta({
// openapi: {
// method: "POST",
// path: "/algolia/search",
// },
// })
.meta({
openapi: {
method: "POST",
path: "/algolia/search",
},
})
.input(algoliaQueries)
.output(z.any())
.query(async ({ ctx, input }) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/router/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ export const collectionRouter = router({

const userId = ctx.auth.userId;

if (!userId) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to create a collection",
});
}

const newCollection = await ctx.prisma.collection.create({
data: {
title,
Expand Down
8 changes: 8 additions & 0 deletions packages/api/src/router/gptApi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { z } from "zod";
import { protectedProcedure, router } from "../trpc";
import {
parseMultipleChoiceResponse,
Expand Down Expand Up @@ -122,7 +123,14 @@ export const gptApiRouter = router({
}),

generateMultipleRandomQuestions: protectedProcedure
.meta({
openapi: {
method: "POST",
path: "/gpt/random-questions",
},
})
.input(multipleRandomQuestionsPromptInput)
.output(z.any())
.mutation(async ({ input }) => {
const { message, numOfQuestions, messageType } = input;

Expand Down
7 changes: 7 additions & 0 deletions packages/api/src/router/pdfTextExtraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ const processOcrResult = (data: any) => {

export const textExtractionRouter = router({
extractText: protectedProcedure
.meta({
openapi: {
method: "POST",
path: "/text-extraction",
},
})
.input(
z.object({
file: z.string(),
fileType: z.string(),
}),
)
.output(z.any())
.mutation(async ({ input }) => {
const { file, fileType } = input;
let ocrResult = "";
Expand Down
9 changes: 7 additions & 2 deletions packages/api/src/router/reviewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import {

export const reviewerRouter = router({
getDiscoverReviewers: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/reviewers/discover",
},
})
.input(highLightReviewersInput)
.output(z.any())
.query(({ ctx, input }) => {
Expand Down Expand Up @@ -219,8 +225,7 @@ export const reviewerRouter = router({
if (isUserPremium && userReviewersCount >= 30) {
throw new TRPCError({
code: "FORBIDDEN",
message:
"Maximum amount of reviewers reached.",
message: "Maximum amount of reviewers reached.",
});
}

Expand Down
21 changes: 20 additions & 1 deletion packages/api/src/router/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const useRouter = router({
.meta({
openapi: {
method: "GET",
path: "/users/me",
path: "/users/me/details",
},
})
.input(z.void())
Expand Down Expand Up @@ -210,6 +210,12 @@ export const useRouter = router({
}),

getTotalPoints: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/users/me/points",
},
})
.input(z.void())
.output(z.any())
.query(async ({ ctx }) => {
Expand All @@ -227,6 +233,12 @@ export const useRouter = router({
}),

getNewBadges: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/users/me/badges/new",
},
})
.input(z.void())
.output(z.any())
.query(async ({ ctx }) => {
Expand Down Expand Up @@ -306,6 +318,12 @@ export const useRouter = router({
}),

getBadges: protectedProcedure
.meta({
openapi: {
method: "GET",
path: "/users/me/badges",
},
})
.input(z.void())
.output(z.any())
.query(async ({ ctx }) => {
Expand Down Expand Up @@ -533,6 +551,7 @@ export const useRouter = router({
path: "/users/me/premium",
},
})
.input(z.void())
.output(z.any())
.mutation(async ({ ctx }) => {
const premiumStatus = await ctx.prisma.user
Expand Down

1 comment on commit 8161827

@vercel
Copy link

@vercel vercel bot commented on 8161827 Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-trek-test – ./apps/nextjs

test-trek-test-dadili.vercel.app
test-trek-test.vercel.app
test-trek-test-git-main-dadili.vercel.app

Please sign in to comment.