代理路由字符串

Proxy Route Strings

我正在尝试制作一个执行此操作的 JS 代理。

routes.api.posts => '/api/posts'
routes.api.posts('postId') => '/api/posts/postId'
routes.api.posts('postId', { name: 'alex' }) => '/api/posts/postId?name=alex'
routes.api.posts({ name: 'alex' }) => '/api/posts?name=alex'

我参考了这些

我已经走到这一步了。

var obj = {
  func: undefined,
  realFunc: (args) => args,
  prop: undefined,
  realProp: true
};

var handlers = {
  path: '',
  get: (target, name) => {
    const prop = target[name];
    if (prop != null) { return prop; }

    let isProp = true;
    Promise.resolve().then(() => {
      handlers.path = `${handlers.path}/${name}`
      console.log('handlers.path', handlers.path)
      if (isProp) {
        console.log(`props ${name}`)
      } else {
        // it's a function
        console.log(`props ${name}`)
      }
    });
    return new Proxy(() => {}, {
      get: handlers.get,
      apply: (a, b) => {
        isProp = false;
        return new Proxy(() => {}, handlers);
      }
    });
  }
};

var routes = new Proxy(obj, handlers)

var case1 = routes.api.posts
console.log(`routes.api.posts()`, case1 === '/api/posts', case1)

var case2 = routes.api.posts('postId')
console.log(`routes.api.posts('postId')`, case2 === '/api/posts/postId', case2)

var case3 = routes.api.posts('postId', { name: 'alex' })
console.log(`routes.api.posts('postId'`, case3 === '/api/posts/postId?name=alex', case3)

var case4 = routes.api.posts({ name: 'alex' })
console.log(`routes.api.posts({ name: 'alex' })`, case4 === '/api/posts?name=alex', case4)

这似乎很有效。 get 拦截丢失的道具并即时创建新的 routes 对象,而 apply 负责函数调用。

let makeRoutes = (prefix = '') => new Proxy(() => {}, {
    get(target, prop) {
        if (prop === 'toString')
            return () => prefix
        if (prop in target || typeof prop !== 'string')
            return target[prop]
        return makeRoutes(prefix + '/' + prop)
    },

    apply(target, thisArg, args) {
        let url = prefix
        for (let a of args)
            url += typeof a === 'object'
                ? '?' + new URLSearchParams(a)
                : '/' + a
        return url
    },
})


let routes = makeRoutes()

console.log('' + routes.api.posts)
console.log('' + routes.api.posts('postId'))
console.log('' + routes.api.posts('postId', {name: 'alex'}))
console.log('' + routes.api.posts({name: 'alex'}))

console.log('' + routes.this.should.work.too('param', {name: 'alex'}))