Closed iceStorm closed 2 months ago
Latest commit: d1f239e858da2c5fd6935d1c89a6d7b6f0a9a0f7
The changes in this PR will be included in the next version bump.
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
@iceStorm is attempting to deploy a commit to the ts-rest Team on Vercel.
A member of the Team first needs to authorize it.
@Gabrola Hii, could you help to assess this enhancement π
Also, we need to access to a route's metadata inside the custom API fetcher callback. Like if a route has metadata with requireAuth: true
then we will attach an Authorization header to the request.
Accessing the raw body and content type
Could you help advice some ideas... @Gabrola
Also, we need to access to a route's metadata inside the custom API fetcher callback. Like if a route has metadata with
requireAuth: true
then we will attach an Authorization header to the request.Accessing the raw body and content type
Could you help advice some ideas... @Gabrola
route
is passed to the API fetcher function
Thank you for the contribution! Can you please add a test to
dsl.spec.ts
? Also your branch is quite a bit behind and is conflicting, can you fix that too?
yess, flying on it π
You also need to merge the types in
ApplyOptions
Updated. I'm not sure if it's aligned or not; it's my first time contributing to OSS. Kindly help to check again π
Issues
0 New issues
0 Accepted issues
Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code
CI is running/has finished running commands for commit d1f239e858da2c5fd6935d1c89a6d7b6f0a9a0f7. 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
Sent with π from NxCloud.
@iceStorm I've just fixed the type merging and add some type tests. Should be good to go. Thank you!
@iceStorm I've just fixed the type merging and add some type tests. Should be good to go. Thank you!
Wow, so fast π
metadata
globally for a project? Like using declare global
, declare module
...Let's say we have a common interface for metadata:
interface ApiRouteMetadata {
Β Β requireAuth?: boolean;
...
}
And we want the interface to automatically be inferred when setting metadata for a route or a router. For consistency.
import { AppRoute as TsRestRoute } from '@ts-rest/core';
export type ApiRouteMetadata {
requireAuth?: boolean;
}
export const SetApiRouteMetadata = (metadata: ApiRouteMetadata) => metadata;
const contract = c.router(
{
getPost: {
method: "GET",
path: "/posts/:id",
responses: {
200: c.type<{ id: number }>(),
},
},
},
{
metadata: SetApiRouteMetadata({ requireAuth: true }),
}
);
The above solution works. But having a way to force the metadata's shape at a global level to strict what a route's metadata can be would make maintenance easier, IMO.
@iceStorm that would be pretty hard to do without extensive refactoring. Module augmentation using declare module
wouldn't work because it can only add properties to interfaces, but not change them. Can't think of a better approach than the one you already presented.
export const SetApiRouteMetadata = <const T extends ApiRouteMetadata>(metadata: T) => metadata;
Doing this should be better, because the individual routes will have literal types such as true
or false
rather than just boolean
.
@iceStorm that would be pretty hard to do without extensive refactoring. Module augmentation using
declare module
wouldn't work because it can only add properties to interfaces, but not change them. Can't think of a better approach than the one you already presented.export const SetApiRouteMetadata = <const T extends ApiRouteMetadata>(metadata: T) => metadata;
Doing this should be better, because the individual routes will have literal types such as
true
orfalse
rather than justboolean
.
Wow, I was curious about how to make the data type from SetApiRouteMetadata
a const. Thank you. Learned a new thing π
In our project, we need a mechanism to set metadata for a whole router (and for a sub-router, of course).
Then routes inside a router can inherit this metadata.
Use case:
requireAuth: true
along with their own metadata.