ts-rest / ts-rest

RPC-like client, contract, and server implementation for a pure REST API
https://ts-rest.com
MIT License
2.12k stars 92 forks source link

feat: create helper functions for route and implementation separation #532

Closed toteto closed 3 months ago

toteto commented 3 months ago

Motivated by few issues (like #521) and comments on Discord, I am adding few methods that will aid into easier creation of reusable endpoints/responses/implementations.

If the changes in ts-res/express are desired, I am willing to try to port them to other server packages.

Resolves #521

changeset-bot[bot] commented 3 months ago

🦋 Changeset detected

Latest commit: 922179e62a54b829c674de0a54af997bbed792c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages | Name | Type | | ----------------------------- | ----- | | @ts-rest/express | Patch | | @ts-rest/core | Patch | | @ts-rest/serverless | Patch | | @ts-rest/example-contracts | Patch | | @ts-rest/non-strict-mode-test | Patch | | @ts-rest/fastify | Patch | | @ts-rest/nest | Patch | | @ts-rest/next | Patch | | @ts-rest/open-api | Patch | | @ts-rest/react-query | Patch | | @ts-rest/solid-query | Patch | | @ts-rest/vue-query | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

vercel[bot] commented 3 months ago

@toteto is attempting to deploy a commit to the ts-rest Team on Vercel.

A member of the Team first needs to authorize it.

sonarcloud[bot] commented 3 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
12.7% Duplication on New Code

See analysis details on SonarCloud

nx-cloud[bot] commented 3 months ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 922179e62a54b829c674de0a54af997bbed792c7. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 4 targets - [`nx affected --target=build --parallel=3`](https://cloud.nx.app/runs/TWDwvWwmiz?utm_source=pull-request&utm_medium=comment) - [`nx affected --target=test --parallel=3 --ci --code-coverage`](https://cloud.nx.app/runs/uKgDaMBUjW?utm_source=pull-request&utm_medium=comment) - [`nx affected --target=lint --parallel=3`](https://cloud.nx.app/runs/Q0xrLo21im?utm_source=pull-request&utm_medium=comment) - [`nx run-many --target=build --projects=ts-rest*`](https://cloud.nx.app/runs/ynooRHPiGe?utm_source=pull-request&utm_medium=comment)

Sent with 💌 from NxCloud.

Gabrola commented 3 months ago

There's no need for the core library helpers since they all exist in the initContract() type helper. makeRoute is covered by c.query() and c.mutation()

For now we'd prefer to stick to the const c = initContract() convention

The express helper can be achieved much easier, so I'll just make a quick to change to cover this need

toteto commented 3 months ago

Having to import initContract or initServer just to use stateless utility methods seems like extra step and extra computation for anyone that is not 100% aware of the source code. Seems like the resulting type of c.mutation/query is not portable as makeMutation or makeQuery would be. This is my opinion at least.

Any opinion on the makeResponses utility?

Gabrola commented 3 months ago

We will be exporting c or similar directly in the next major version, and the old stateless method will be removed. However, exporting the utilities separately, not contained in an object, makes discovery harder and dependency on the docs even more - think of zod's z