Closed miladhashemi closed 3 years ago
There's no APIs to get any kind of configurations and/or underlying session data in the client. This is mostly for security reasons.
If you want to share things between your client and server you have a few options.
Note that if you're planning on using secrets or confidential information, then you probably want to use the last solution (HTTP API). The other solutions will bake everything into the build bundles.
export default {
myNamespace: {
attributes: {
endpoint: 'foo'
}
}
}
core.config('myNamespace.attributes.endpoint')
# Create a common package
mkdir src/common
cd src/common
npm init
edit config.json
# Use in server
cd src/server
npm install ../common
edit config.js
# Use in client
cd src/client
npm install ../common
edit config.js
Example configuration usage:
// common config
{
"mountpoint": {
"attributes": {
"endpoint": "foo"
}
}
}
// server
const common = require('the-name-you-gave-your-package/config.json')
module.exports = {
vfs: {
mountpoints: [{
attributes: {
...common.mountpoint.attributes,
username: 'foo'
}
}]
}
}
// client
import * as common from 'the-name-you-gave-your-package/config.json'
export default {
myNamespace: common
}
// client usage
core.config('myNamespace.attributes.endpoint')
I've already shown you how to do API endpoints in another issue. You can get the config on the server via the following and send it as a http response or whatever:
const adapters = core.config('mountpoints') // OR: core.make('osjs/vfs').mountpoints to get more information (but probably not relevant in your case)
const found = mountpoints.find(mount => mount.name === 'myMonster')
res.send(found.attributes.endpoint)
If you go down the API endpoint route you can do a bit more generic approach if you need to access more configuration stuff down the line.
routeAuthenticated('GET', '/api/config/:path', (req, res) => {
res.json(core.config(req.params.path))
})
Then to get any arbitrary value(s) from the tree you can just do core.request('/api/config/vfs.root')
.
Not really sure I'd recommend anything like this, but hey, it might be OK in your use case.
Edit Tip: This also works core.config('vfs.mountpoints.0.attributes')
if you're confident that the order does not change :)
Thank U indeed. It was really complete. I think the last solution (http api
) would be best practice.
Also your common config
solution nearly bring to mind php yii2 advanced framework architecture that could be effortless and sweet solution to access common config (In osjs between server/client
and in yii2/advanced backend\frontend
).
With this deference that in yii2/advanced
, frontend
and backend
are independent applications.
YII really brings back some (not quite good) memories :sweat_smile:
Since we more or less figured out something here; I'll close this issue. Feel free to re-open if you have any problems with the implementation :)
I've already shown you how to do API endpoints in another issue
My bad. This was from another user :blush: I was thinking of this discussion thread:
https://github.com/os-js/osjs-server/issues/41#issuecomment-729793912
https://github.com/os-js/osjs-server/issues/41#issuecomment-729800045
In a application i need
attributes
in server vfs. How can i get a vfs adapter attributes that mounted to osjs, from client?for example i want
endpoint
in client. Is it possible to get it in server.js and send it to index.js?