ts-rest / ts-rest

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

fix: ts error when using the tsr utility on createNextHandler #563

Closed bnsngltn closed 2 months ago

bnsngltn commented 2 months ago

Hello, thanks for the awesome release that adds support to Next App Router.

Everything works great but what I would like to achieve is to have multiple routers and merge them together as I instantiate the handler. I have noticed that the serverless package does not have any docs yet and that the linked example was very bare.

Basically what I'm trying to achieve is something like this:

The sample contract

import { initContract } from '@ts-rest/core';
import { z } from 'zod';

const c = initContract();

export const contract = c.router({
  hello: {
    greet: {
      method: 'GET',
      path: '/hello/greet',
      description: 'Say hello',
      responses: {
        200: z.object({
          message: z.string(),
        }),
      },
    },
  },
});

Then I wanted to define a separate router under the hello namespace using the tsr utility I have found in the source code. It was exported, but I could not find any usage to it, so I assumed it's a utility function for us the end users. Correct me if I'm wrong

import { tsr } from '@ts-rest/serverless/next';
import { contract } from './contract';

export const helloRouter = tsr.router(testContract.hello, {
  greet: async () => {
    return { status: 200, body: { message: 'Hi' } };
  },
});

I then get a TypeScript Error router when trying to put the helloRouter under my handler on my route handler definition saying that my helloRouter cannot be assigned to the expected router shape of the createNextHandler function.

// app/api/[...ts-rest]/route.ts

import { createNextHandler } from '@ts-rest/serverless/next';

const handler = createNextHandler(contract, {
   hello: helloRouter // ❗TypeScript error here
}, {
    basePath: '/api',
    jsonQuery: true,
    responseValidation: true,
    handlerType: 'app-router',

})

Note: This only occurs when strict is set to true in tsconfig.json

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: 31ae32ff92c3fcfeccf9e3f213f92010be965f01

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/serverless | Patch | | @ts-rest/example-contracts | Patch | | @ts-rest/non-strict-mode-test | Patch | | @ts-rest/core | Patch | | @ts-rest/express | 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 2 months ago

@bnsngltn 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 2 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

nx-cloud[bot] commented 2 months ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 31ae32ff92c3fcfeccf9e3f213f92010be965f01. 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 5 targets - [`nx affected --target=build --parallel=3`](https://cloud.nx.app/runs/Z2uRBRdKnl?utm_source=pull-request&utm_medium=comment) - [`nx run-many --target=build --projects=ts-rest*`](https://cloud.nx.app/runs/C9Q9QrtQ4R?utm_source=pull-request&utm_medium=comment) - [`nx affected --target=test --parallel=3 --ci --code-coverage`](https://cloud.nx.app/runs/peTKxaLVOR?utm_source=pull-request&utm_medium=comment) - [`nx affected --target=lint --parallel=3`](https://cloud.nx.app/runs/htYdv3WaRD?utm_source=pull-request&utm_medium=comment) - [`nx run-many --target=build`](https://cloud.nx.app/runs/H5YkUZTAea?utm_source=pull-request&utm_medium=comment)

Sent with 💌 from NxCloud.

Gabrola commented 2 months ago

Thank you!