codewillclick / obparity

simple node.js library for creating objects that exist in tandem between client (browser) and server
MIT License
0 stars 0 forks source link

Create fetchUrl management object middleware for express.js #8

Closed codewillclick closed 3 years ago

codewillclick commented 3 years ago

I think if I make a subdirectory with its own package.json, but don't include "./express-middleware" in the root package.json's dependencies, npm should... not install it manually. Which is ideal, I guess?

Well, I'll think about should/should-not later. For now, an object that takes in (req,res) => ... and manages routing to and from individual ParityObjects would be useful.

Right now, I'm putting these path checks directly into app.get() and app.post(). But what about when I need to dynamically generate a bunch of ParityObjects? Not everything's going to be a single-page, single-user application. Something would have to manage that.

codewillclick commented 3 years ago

Also might not be a bad idea to have a management object that does not assume express.js.

codewillclick commented 3 years ago

What, a single ParityObject that manages a ton of others? Mmm... ... Wouldn't have to be a ParityObject, then. But if it also handles passing the initial url into its managed objects...

I guess the idea would be to have a portion of the url be generated automatically. Like for one-object-per-user-session kind of things. You have a lot of users, you have a lot of objects. And each one needs its slightly different url, or id it passes into its POST data.

So it'd, what, be a factory object, too?

codewillclick commented 3 years ago

Hmmmmm... Maybe make that a static method for ParityObject? It returns a url-managing factory object for its specific class. Automatically returns a new WhateverObject(url). I guess that'd look like...

new this(generatedUrl)

So maybe something like...

let manager = ParityObject.createManager(baseUrl)
// At some point...
let ob = manager.createObject()
// Which is used for whatever, and then later again...
app.post(baseUrl+'/*', (req,res) => {
    let a = manager.evaluate(req.url,req.body)
    if (a)
        return res.send(a)
    ...
})
codewillclick commented 3 years ago

Holy Freaking Moly.

This Magager class took a lot of debugging to get to a functional (if unproven) state.

Here's the diff.

I absolutely need a wall of unit tests for this whiny boy.

Incidentally, it does not rely on express.js. Its use looks like...

app.post('/op', async (req,res) => {
    ...
    let a = await manager.evaluate(req.url, req.body)
    ...
})

... which means it works with anything so long as it's provided a url and the usual parity client->server object.

codewillclick commented 3 years ago

Oh right, I added in a proper express.js middleware function. Diff's here.