Skip to content

Commit

Permalink
moved symbols to global Symbol scope so you can have and use multiple…
Browse files Browse the repository at this point in the history
… instances of Metro

request proxy get now checks if a property is a function, if so, it binds it to the target, otherwise you get an error
added checks in bodyProxy before assuming body is an object
switched to esbuild
  • Loading branch information
poef committed Jan 4, 2025
1 parent 1e0961f commit 134c555
Show file tree
Hide file tree
Showing 7 changed files with 1,396 additions and 30 deletions.
641 changes: 640 additions & 1 deletion dist/browser.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion dist/browser.js.map

Large diffs are not rendered by default.

699 changes: 698 additions & 1 deletion dist/everything.js

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion dist/everything.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@muze-nl/metro",
"version": "0.4.4",
"version": "0.5.0",
"description": "http client with middleware support",
"type": "module",
"source": [ "src/browser.mjs", "src/everything.mjs" ],
Expand All @@ -18,7 +18,8 @@
"scripts": {
"test": "tap test/*.mjs",
"tap": "tap",
"build": "parcel build"
"build-dev": "esbuild --bundle src/browser.mjs --outfile=dist/browser.js --sourcemap; esbuild --bundle src/everything.mjs --outfile=dist/everything.js --sourcemap",
"build": "esbuild --bundle src/browser.mjs --outfile=dist/browser.min.js --minify --sourcemap; esbuild --bundle src/everything.mjs --outfile=dist/everything.min.js --minify --sourcemap"
},
"repository": {
"type": "git",
Expand Down
61 changes: 39 additions & 22 deletions src/metro.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const metroURL = 'https://metro.muze.nl/details/'
* - isProxy: used to test if an object is a metro Proxy to another object
* - source: used to return the actual source (target) of a metro Proxy
*/
export const symbols = {
isProxy: Symbol('isProxy'),
source: Symbol('source')
if (!Symbol.metroProxy) {
Symbol.metroProxy = Symbol('isProxy')
}
if (!Symbol.metroSource) {
Symbol.metroSource = Symbol('source')
}

/**
Expand Down Expand Up @@ -122,11 +124,11 @@ class Client

const metrofetch = async function browserFetch(req)
{
if (req[symbols.isProxy]) {
if (req[Symbol.metroProxy]) {
// even though a Proxy is supposed to be 'invisible'
// fetch() doesn't work with the proxy (in Firefox),
// you need the actual Request object here
req = req[symbols.source]
req = req[Symbol.metroSource]
}
const res = await fetch(req)
return response(res)
Expand Down Expand Up @@ -248,10 +250,10 @@ function bodyProxy(body, r)
return new Proxy(source, {
get(target, prop, receiver) {
switch (prop) {
case symbols.isProxy:
case Symbol.metroProxy:
return true
break
case symbols.source:
case Symbol.metroSource:
return body
break
case 'toString':
Expand All @@ -260,7 +262,7 @@ function bodyProxy(body, r)
}
break
}
if (typeof body == 'object') {
if (body && typeof body == 'object') {
if (prop in body) {
if (typeof body[prop] == 'function') {
return function(...args) {
Expand All @@ -282,13 +284,25 @@ function bodyProxy(body, r)
}
},
has(target, prop) {
return prop in body
if (body && typeof body == 'object') {
return prop in body
} else {
return prop in target
}
},
ownKeys(target) {
return Reflect.ownKeys(body)
if (body && typeof body == 'object') {
return Reflect.ownKeys(body)
} else {
return Reflect.ownKeys(target)
}
},
getOwnPropertyDescriptor(target, prop) {
return Object.getOwnPropertyDescriptor(body,prop)
if (body && typeof body == 'object') {
return Object.getOwnPropertyDescriptor(body,prop)
} else {
return Object.getOwnPropertyDescriptor(target,prop)
}
}
})
}
Expand Down Expand Up @@ -384,10 +398,10 @@ export function request(...options)
return new Proxy(r, {
get(target, prop, receiver) {
switch(prop) {
case symbols.source:
case Symbol.metroSsource:
return target
break
case symbols.isProxy:
case Symbol.metroProxy:
return true
break
case 'with':
Expand Down Expand Up @@ -423,13 +437,16 @@ export function request(...options)
body = target.body
}
if (body) {
if (body[symbols.isProxy]) {
if (body[Symbol.metroProxy]) {
return body
}
return bodyProxy(body, target)
}
break
}
if (target[prop] instanceof Function) {
return target[prop].bind(target)
}
return target[prop]
}
})
Expand Down Expand Up @@ -498,10 +515,10 @@ export function response(...options)
return new Proxy(r, {
get(target, prop, receiver) {
switch(prop) {
case symbols.isProxy:
case Symbol.metroProxy:
return true
break
case symbols.source:
case Symbol.metroSource:
return target
break
case 'with':
Expand All @@ -511,7 +528,7 @@ export function response(...options)
break
case 'body':
if (responseParams.body) {
if (responseParams.body[symbols.isProxy]) {
if (responseParams.body[Symbol.metroProxy]) {
return responseParams.body
}
return bodyProxy(responseParams.body, target)
Expand Down Expand Up @@ -621,10 +638,10 @@ export function url(...options)
return new Proxy(u, {
get(target, prop, receiver) {
switch(prop) {
case symbols.isProxy:
case Symbol.metroProxy:
return true
break
case symbols.source:
case Symbol.metroSource:
return target
break
case 'with':
Expand Down Expand Up @@ -681,10 +698,10 @@ export function formdata(...options)
return new Proxy(params, {
get: (target,prop,receiver) => {
switch(prop) {
case symbols.isProxy:
case Symbol.metroProxy:
return true
break
case symbols.source:
case Symbol.metroSource:
return target
break
case 'with':
Expand Down Expand Up @@ -768,7 +785,7 @@ export const trace = {
metroConsole.info(req?.url, req, middleware)
},
response: (res, middleware) => {
metroConsole.info(res?.body ? res.body[symbols.source]: null, res, middleware)
metroConsole.info(res?.body ? res.body[Symbol.metroSource]: null, res, middleware)
metroConsole.groupEnd(group)
group--
}
Expand Down
4 changes: 2 additions & 2 deletions src/mw/json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default function jsonmw(options) {
'Accept':'application/json'
}
})
if (req.body && typeof req.body[metro.symbols.source] == 'object') {
if (req.body && typeof req.body[Symbol.metroSource] == 'object') {
req = req.with({
body: JSON.stringify(req.body[metro.symbols.source], options.replacer, options.space)
body: JSON.stringify(req.body[Symbol.metroSource], options.replacer, options.space)
})
}
} else {
Expand Down

0 comments on commit 134c555

Please sign in to comment.