Git Product home page Git Product logo

Comments (8)

idiotWu avatar idiotWu commented on May 10, 2024

Some potential issues of chainable baseURL:

1. Syntax

Should relative paths be prefixed with '.'?

const { router } = createServer('/api');

router.base('/v1'); 
// OR
router.base('./v1');

P.S. The native URL accepts both relative and absolute paths (notice the trailing slash):

new URL('/v1', 'http://localhost:3000/api'); // => http://localhost:3000/v1
new URL('./v1', 'http://localhost:3000/api'); // => http://localhost:3000/v1
new URL('./v1', 'http://localhost:3000/api/'); // => http://localhost:3000/api/v1

2. Possible confusion

Think about the following scenario: router.base() points to another domain:

const { router } = createServer('/api'); // => http://localhost:3000/api

router.base('http://remote.com'); // => http://remote.com

from service-mocker.

vincentbel avatar vincentbel commented on May 10, 2024

@idiotWu Summary of some of our discussions:

  1. Servers with different origin should use createServer, rather than router.base(because they are different servers, router.base is counter-intuitive)
// Good
const apiRouter = createServer('/api').router
const githubApiRouter = createServer('https://api.github.com').router

// Bad
const server = createServer('/api')
const apiRouter = server.router
const githubApiRouter = server.router.base('/https://api.github.com')

// Also bad
const server = createServer()
const apiRouter = server.router.base('/api')
const githubApiRouter = server.router.base('/https://api.github.com')
  1. Chainable baseURL can also be used to split routes. Example: https://gist.github.com/VincentBel/6a7d35e6a2999e2aa90d065a77831bfd

Feel free to point out faults if exists.

from service-mocker.

idiotWu avatar idiotWu commented on May 10, 2024

@vincentbel so the given baseURL should be regarded as a kind of "prefixes" for the path?

Ah, we have already documented it as "prefixes":

screenshot

So I think this issue is more a bug fix than a breaking feature?

from service-mocker.

vincentbel avatar vincentbel commented on May 10, 2024

@idiotWu yeah, I think we don't provide router.base as a way to create a new server in purpose. So I also think it is more a bug fix.

from service-mocker.

idiotWu avatar idiotWu commented on May 10, 2024

@vincentbel one more thing, as we've discussed, the router.base() method is designed for "prefixes", or we can say relative paths only. So should we reject all absolute paths or just throw a warning?

router.base('http://a.com'); // die or show warning?

from service-mocker.

vincentbel avatar vincentbel commented on May 10, 2024

@idiotWu I think die is better because we don't mean to support this approach.😑

from service-mocker.

vincentbel avatar vincentbel commented on May 10, 2024

and warnings are just ignored by developers. 😅

from service-mocker.

idiotWu avatar idiotWu commented on May 10, 2024

Resolved in #23 #25

from service-mocker.

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.