Giter Club home page Giter Club logo

Comments (4)

RobinTail avatar RobinTail commented on July 28, 2024

Hello @linde12 .

Thank you for your feedback. I am happy to answer your questions in order.

I can't find any documentation on how you access & validate express route path values. E.g. express.get('/users/:id', ...) - how do i set a schema for id and access its value?

You can find the complete answer here: #73
Basically you should do this:

const myRouting: Routing = {
  users: {
    ':id': myEndpoint // <— connected to /users/:id 
  }
};

And in order to obtain the value of :id you need a paramsProviderMiddleware. You will find an example at the link mentioned above.

I can't find any documentation on how you work with array outputs [1]

Shortly: you should pass the array to an object property.

// instead of
return [1,2,3];
// do this
return {
  myData: [1,2,3]
}

This is an intentional limitation: the output of the Endpoint should be z.object() or union/intersection of objects.
The error [1] you get is about this constraint.
Besides that it's usually better to return objects from the beginning. As you develop and further develop your API, you may need to add other data or meta information. This way you can maintain backward compatibility for longer.

I can't seem to create an endpoint without any inputs, e.g. a GET route with not query params - is this a conscious decision that you always have to define an input schema? Can't find any documentation on it.

That should be easy. Please try this:

const myEndpoint = myEndpointsFactory.build({
  method: 'get',
  input: z.object({}),
  ...
});

So just use an empty object schema. The object being validated as input in case of GET request is req.query of express.
By the way, you can use the same pattern for middlewares without inputs.

When using DependsOnMethod i can specify a certain endpoint for e certain method, but i still have to set the method property of the endpoint. Concious decision? [2]

Yes. This is to make it possible to implement many-to-many relationships between the available route methods and the supported handler methods. Here is an example of such case:

// example of different I/O schemas for /v1/user
const routing: Routing = {
  v1: {
    user: new DependsOnMethod({
      get: myEndpointA, // GET —> handler for GET and DELETE
      delete: myEndpointA, // DELETE —> handler for GET and DELETE
      post: myEndpointB, // POST —> handler for POST and PATCH
      patch: myEndpointB, // PATCH —> handler for POST and PATCH
    })
  }
};

There is no example of a POST with query parameters. Is this not supported? I know of several APIs that use this and since im doing some vetting i just want to know :-)

You're right. There is no such example. Currently express-zod-api is processing only .body (and optionally .files) of the POST requests. It is usually better to transfer all the data in one way and in the case of POST taking advantage of the body. However, I think I can ponder this and maybe add an extra option in the config so you can receive both .query and .body to input.

Please let me know if I managed to provide you with required answers or something need to be additionally clarified.

from express-zod-api.

linde12 avatar linde12 commented on July 28, 2024

Hi @RobinTail ! Thank you for your in-depth reply! I am clear on all things except:

When using DependsOnMethod i can specify a certain endpoint for e certain method, but i still have to set the method property of the endpoint. Concious decision? [2]

Yes. This is to make it possible to implement many-to-many relationships between the available route methods and the supported handler methods. Here is an example of such case:

// example of different I/O schemas for /v1/user
const routing: Routing = {
  v1: {
    user: new DependsOnMethod({
      get: myEndpointA, // GET —> handler for GET and DELETE
      delete: myEndpointA, // DELETE —> handler for GET and DELETE
      post: myEndpointB, // POST —> handler for POST and PATCH
      patch: myEndpointB, // PATCH —> handler for POST and PATCH
    })
  }
};

I think i was a bit unclear in my question, but i'm wondering if i still have to set the method or methods property on myEndpointA even when using it with DependsOnMethod cause when i didnt i would get a typescript error. Looking back i don't see this as an issue anyway 😄 Thank you again!

from express-zod-api.

RobinTail avatar RobinTail commented on July 28, 2024

Hello @linde12 .

...i'm wondering if i still have to set the method or methods property on myEndpointA even when using it with DependsOnMethod...

Yes, you have to.
The Endpoint exists regardless of Routing. Basically, it "does not know" which route you're connecting it to, or even you are not connecting it at all.
In this regard, you should consider DependsOnMethod as a Routing helper, and not an Endpoint. At least that's the design at the moment.

I hope I managed to clarify this topic for you.
Please let me know if you have any other questions on the issue.

from express-zod-api.

RobinTail avatar RobinTail commented on July 28, 2024

Hello @linde12 ,

I made a PR on the new config option that I believe will help you to achieve the desired behavior: including req.query to input along with req.body for POST requests.

The feature will be released in version 2.8.0. The issue will be converted to discussion.

from express-zod-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.