Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to create a single endpoint definition #149

Closed
Todomir opened this issue Sep 22, 2022 · 4 comments
Closed

Function to create a single endpoint definition #149

Todomir opened this issue Sep 22, 2022 · 4 comments

Comments

@Todomir
Copy link

Todomir commented Sep 22, 2022

We currently have an file with a bunch of route definitions. The readability really starts to get worse as the file grow, having an makeEndpoint method would be specially useful when we want one definition per file, e.g:

// getUser.ts

export default makeEndpoint({
  method: "get",
  path: "/users/:id",
  alias: "getUser",
  description: "Get a user",
  response: z.object({
    id: z.number(),
    name: z.string(),
  }),
})
// createUser.ts

export default makeEndpoint({
  method: "post",
  path: "/users",
  alias: "createUser",
  description: "Create a user",
  parameters: [{
    name: 'body',
    type: 'Body',
    schema: z.object({
      name: z.string(),
    })
  }],
  response: z.object({
    id: z.number(),
    name: z.string(),
  }),
})
// api.ts
import getUser from './getUser';
import createUser from './createUser';

// It could also be compatible `makeApi` as well.
const api = new Zodios([getUser, createUser])
@ecyrbe
Copy link
Owner

ecyrbe commented Sep 22, 2022

Hello,
Thank you for the feedback,

This already possible with apiBuilder. Maybe it's not clear enough in the docs that this is possible to split definitions:

// getUser.ts

export default apiBuilder({
  method: "get",
  path: "/users/:id",
  alias: "getUser",
  description: "Get a user",
  response: z.object({
    id: z.number(),
    name: z.string(),
  }),
}).build();
// createUser.ts

export default apiBuilder({
  method: "post",
  path: "/users",
  alias: "createUser",
  description: "Create a user",
  parameters: [{
    name: 'body',
    type: 'Body',
    schema: z.object({
      name: z.string(),
    })
  }],
  response: z.object({
    id: z.number(),
    name: z.string(),
  }),
}).build();
// api.ts
import getUser from './getUser';
import createUser from './createUser';

const api = new Zodios([...getUser, ...createUser]);

You can check dev.to for a full example.
And documentation

Anyway, i think it's a good idea to also have a dedicated helper to create an individual endpoint only and this way evoid using the spread operator to combine them.

Will do!

@ecyrbe
Copy link
Owner

ecyrbe commented Sep 22, 2022

Done!

Check Release v9.2.0
And documentation : https://www.zodios.org/docs/api/helpers#makeendpoint

@ecyrbe ecyrbe closed this as completed Sep 22, 2022
@Todomir
Copy link
Author

Todomir commented Sep 22, 2022

Wow, talk about blazingly fast. Thanks 🚀

@ecyrbe
Copy link
Owner

ecyrbe commented Sep 22, 2022

if not already, consider giving zodios project a star, it helps the project to be improved to make it known as it means more feedback like yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants