|
| 1 | +This project is organized as follows: |
| 2 | + |
| 3 | +- `apps` This directory is the home of all applications. |
| 4 | +- `packages` This directory is the home of all shared code, used by more than one app. |
| 5 | + |
| 6 | +We use `pnpm` to manage this monorepo. |
| 7 | + |
| 8 | +# apps/api |
| 9 | + |
| 10 | +The following describes the `apps/api` project. |
| 11 | + |
| 12 | +- `src/global` This directory is for functionality common across all API versions. If you need to share a utility, constant, type etc across multiple versions of the Sort API, put that here. Top level / unversioned API routes (e.g. `/robots.txt`) belong here too. |
| 13 | +- `src/v2` Version two of the Sort API. |
| 14 | + |
| 15 | +Each of the above directories follow the following subdirectory structure: |
| 16 | + |
| 17 | +- `/constants` Version specific values which are not intended to change. |
| 18 | +- `/controllers` Version specific business logic. |
| 19 | +- `/mocks` Version specific mocks used in tests. |
| 20 | +- `/routes` Version specific API route definitions which map public API routes to controllers. |
| 21 | +- `/services` Version specific 3rd party service abstractions. This is where your API version specific Database/ChatGPT/Discord etc abstractions belong. |
| 22 | +- `/types` Version specific TypeScript types. |
| 23 | +- `/utils` Version specific shared helpers. |
| 24 | + |
| 25 | +All mocks, services, types and utils should be added to `packages/shared` unless |
| 26 | +intended to _only_ be used in the API. |
| 27 | + |
| 28 | +## File names |
| 29 | + |
| 30 | +File naming convention is: `lower-case.topic.ts`. |
| 31 | + |
| 32 | +- Words are lowercase and separated with `-`. |
| 33 | + |
| 34 | +### Topics |
| 35 | + |
| 36 | +File names use the `topic` naming convention, where the singular version of the |
| 37 | +top-level version directory name (`global`, `v2`, etc) is placed at the end of the filename before the |
| 38 | +file extention. This makes it easier to see the purpose of each file when you |
| 39 | +have multiple files open at the same time. |
| 40 | + |
| 41 | +``` |
| 42 | +v2/ |
| 43 | +├─ services/ |
| 44 | +│ ├─ user.service.ts |
| 45 | +├─ controllers/ |
| 46 | +│ ├─ user.controller.ts |
| 47 | +│ ├─ organizations/ |
| 48 | +│ │ ├─ invite.controller.ts # Note "controller" not "organization" |
| 49 | +├─ routes/ |
| 50 | +│ ├─ user.route.ts |
| 51 | +``` |
| 52 | + |
| 53 | +### Test file names |
| 54 | + |
| 55 | +Tests are colocated in the same directory as the functionality. Test file names |
| 56 | +are identical to the file they are testing except they end with `.test.ts`. |
| 57 | +Example: |
| 58 | + |
| 59 | +``` |
| 60 | +services/ |
| 61 | +├─ user.service.ts |
| 62 | +├─ user.service.test.ts # <-- Contains the tests for the functionality in user.service.ts |
| 63 | +``` |
| 64 | + |
| 65 | +## Snake case |
| 66 | + |
| 67 | +API input and response fields must use `snake_case`. |
| 68 | + |
| 69 | +## Test users |
| 70 | + |
| 71 | +If you are testing the [UI][] and the API together locally, you'll first need |
| 72 | +to seed your local database with our test user. Run the following command from |
| 73 | +the `apps/api` directory: |
| 74 | + |
| 75 | +```sh |
| 76 | +pnpm db:reset |
| 77 | +``` |
| 78 | + |
| 79 | +This will run the latest version of the database locally and add our test user account to it. Now you can |
| 80 | +[log in through your local UI][local-login] without trouble. |
| 81 | + |
| 82 | +## OpenAPI |
| 83 | + |
| 84 | +We generate an [OpenAPI spec](https://swagger.io/specification/) from our route/controller schemas. The spec is part |
| 85 | +of our public product offering so care should be taken to ensure it's accuracy. |
| 86 | +Follow these guidelines when added or editing API endpoints. |
| 87 | + |
| 88 | +### 1. OpenAPI descriptions |
| 89 | + |
| 90 | +The values for all OpenAPI endpoint descriptions are found in the `src/docs` |
| 91 | +directory. Each endpoint has a file in this directory named after it's |
| 92 | +`operationId` and suffixed with `.md`. Using dedicated markdown files allows us |
| 93 | +to get full markdown syntax highlighting in our editor and no need for escaping |
| 94 | +strings etc. |
| 95 | + |
| 96 | +For example, if the `operationId` is `list_changes`, then the documentation file |
| 97 | +is found in `apps/api/src/docs/list_changes.md`. |
| 98 | + |
| 99 | +The values of these files are included in the generated OpenAPI spec automatically. |
| 100 | + |
| 101 | +If you are adding a new endpoint, make sure to add a description file. |
| 102 | + |
| 103 | +Guidelines for writing descriptions: A full sentence or paragraph(s) describing |
| 104 | +to developers the purpose of the endpoint and anything helpful. Use markdown in |
| 105 | +this field for things like links to our docs.sort.xyz site. End each sentence |
| 106 | +with a period. Take a look at some other files for examples. |
| 107 | + |
| 108 | +### 2. Ensure the endpoint schema includes the following fields |
| 109 | + - `operationId` A unique name for the endpoint |
| 110 | + - `summary` A sentence fragment about what the endpoint does. Do not end with a period. |
| 111 | + |
| 112 | +For `summary` and `description` fields, capitalize our product entities like `Organization`, |
| 113 | +`Issue` and `Change Request`. |
| 114 | + |
| 115 | +Example |
| 116 | + |
| 117 | +```ts |
| 118 | +export const getOrganizationBySlugSchema = { |
| 119 | + headers: AuthHeadersSchema, |
| 120 | + params: ParamsSchema, |
| 121 | + summary: 'Get a Sort Organization', |
| 122 | + // description -> This comes from src/docs/get_organization.md |
| 123 | + operationId: 'get_organization', |
| 124 | + response: { |
| 125 | + 200: createMessageSchema( |
| 126 | + 'get_organization', |
| 127 | + Type.Object({ |
| 128 | + organization: OrganizationSchema |
| 129 | + }) |
| 130 | + ), |
| 131 | + 400: ValidationErrorSchema, |
| 132 | + 401: GeneralErrorSchema, |
| 133 | + 500: GeneralErrorSchema |
| 134 | + } |
| 135 | +} satisfies FastifySchema |
| 136 | +``` |
| 137 | + |
| 138 | +You can preview what this will look like by running the API locally and visiting: http://localhost:8080/docs/static/index.html |
| 139 | + |
| 140 | +### 2. Hide endpoints which are not ready or not intended for public use |
| 141 | + |
| 142 | +You can prevent endpoints from being included in our public OpenAPI spec by |
| 143 | +setting `hide: true` in the endpoint schema. |
| 144 | + |
| 145 | +```ts |
| 146 | +export const getMyHiddenThingsSchema = { |
| 147 | + headers: AuthHeadersSchema, |
| 148 | + params: ParamsSchema, |
| 149 | + summary: 'Get something hidden from the public', |
| 150 | + description: 'Gets hidden stuff.', |
| 151 | + operationId: 'getMyHiddenThings', |
| 152 | + hide: true, // <=== |
| 153 | + ... |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +[UI]: https://github.com/sortxyz/sorthub |
| 158 | +[local-login]: https://github.com/sortxyz/sorthub#common-issues |
| 159 | + |
| 160 | +# apps/worker |
| 161 | + |
| 162 | +The following describes the `apps/worker` project. |
| 163 | + |
| 164 | +All mocks, services, types and utils should be added to `packages/shared` unless |
| 165 | +intended to _only_ be used in the worker. |
0 commit comments