How to deal with multiple methods that share same path? #362
-
I'm creating Zodios Client for Mongo Atlas Data API. They have a method
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hello, Only way at the moment is to make the path distinguishable or make the returned value an union of the possible results. If you want to do path distinguishable and not use response unions, you can try adding a dummy query parameter. import { Zodios } from "@zodios/core";
import { z } from "zod";
import { UserSchema, ArticleSchema} from "./schema";
export const atlasDataClient = new Zodios(
"https://us-east-1.aws.data.mongodb-api.com/app/data-12345/endpoint",
[ {
method: "post",
path: "/data/v1/action/findOne?actionFor=user",
alias: "findOneUser",
description: "Find one user",
parameters: [
{
name: "body",
type: "Body",
schema: z.object({
dataSource: z.string(),
database: z.string(),
collection: z.string(),
filter: z.object({
_id: z.string(),
}),
}),
},
],
response: z.object({
document: UserSchema.nullable(),
}),
},
{
method: "post",
path: "/data/v1/action/findOne?actionFor=article",
alias: "findOneArticle",
description: "Find one article",
parameters: [
{
name: "body",
type: "Body",
schema: z.object({
dataSource: z.string(),
database: z.string(),
collection: z.string(),
filter: z.object({
_id: z.string(),
}),
}),
},
],
response: z.object({
document: ArticleSchema.nullable(),
}),
}]
) Or if your API rejects on dummy query parameters, you can try to distinguish your path with a path param, like this : import { Zodios } from "@zodios/core";
import { z } from "zod";
import { UserSchema, ArticleSchema} from "./schema";
export const atlasDataClient = new Zodios(
"https://us-east-1.aws.data.mongodb-api.com/app/data-12345/endpoint",
[ {
method: "post",
path: "/data/v1/:userAction/findOne",
alias: "findOneUser",
description: "Find one user",
parameters: [
{
name: "userAction",
type: "Path",
schema: z.literal('action').default('action'); // this will prevent submitting anything else than action
},
{
name: "body",
type: "Body",
schema: z.object({
dataSource: z.string(),
database: z.string(),
collection: z.string(),
filter: z.object({
_id: z.string(),
}),
}),
},
],
response: z.object({
document: UserSchema.nullable(),
}),
},
{
method: "post",
path: "/data/v1/:articleAction/findOne",
alias: "findOneArticle",
description: "Find one article",
parameters: [
{
name: "articleAction",
type: "Path",
schema: z.literal('action').default('action'); // this will prevent submitting anything else than action
},
{
name: "body",
type: "Body",
schema: z.object({
dataSource: z.string(),
database: z.string(),
collection: z.string(),
filter: z.object({
_id: z.string(),
}),
}),
},
],
response: z.object({
document: ArticleSchema.nullable(),
}),
}]
) default |
Beta Was this translation helpful? Give feedback.
-
Hello @ecyrbe I am wondering if this will be fixed in the future? I ran through the same problem, where I have the same API point, and was getting the error, I fixed it with the second solution like this I am wondering though, if there are any drawbacks of using the sendDefaults? I have it like this for every call at the moment, but so far haven't seen anything different any thoughts on this? |
Beta Was this translation helpful? Give feedback.
Hello,
Only way at the moment is to make the path distinguishable or make the returned value an union of the possible results.
Indeed, internaly the path is the only way to dinstinguish which api endpoint to make and which validation to execute.
If you want to do path distinguishable and not use response unions, you can try adding a dummy query parameter.
Indeed usually APIs don't reject unused query parameters :