zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 89 forks source link

REST handler should detect protocol, host, port automatically for the endpoint #737

Open vbg8exadel opened 1 year ago

vbg8exadel commented 1 year ago

Description and expected behavior As per documentation in order to use REST Handler we need to provide endpoint URL. Unfortunately the only possible format is absolute URL with hardcoded protocol, hostname and port number. See the documentation at https://zenstack.dev/docs/reference/server-adapters/api-handlers/rest

handler: RestApiHandler({ endpoint: 'http://myhost/api' }) 

While testing locally it is enough to have endpoint configured as so, but once you start deploying the same code to multiple environments, then it becomes invalid. The protocol, host, port for the endpoint URL should be detected automatically so we can add the URL context only. For example, like so:

handler: RestApiHandler({ endpoint: '/api' }) 

After that I can run the server locally at http://localhost:3000, or deploy to DEV server which runs at https://dev-api.myhost.org:8080: my code will work properly, and the URLs will be pointing to correct place.

Environment (please complete the following information):

ymc9 commented 1 year ago

Hi @vbg8exadel , sorry for the late response. I understand it's not convenient. One possible solution is to put the deployment URI into an environment variable and use it to configure the API handler. IIRC, the full URL is required by JSON:API specification.

Maybe we can improve this by inspecting the request object and extracting the current base URL out of it.

vbg8exadel commented 1 year ago

Yes, that is one option. Although there should be a way to ger it from Express router. I hope there is a way to get the base URL once in the beginning.