Uranio TRX extends Uranio API with methods that can be called from the client, the same sever or another server.
This methods are called Hooks and are auto-generated by URANIO CLI.
If the following Atom
is defined:
// src/atoms/product/index.ts
import uranio from 'uranio';
export default uranio.register.atom({
plural: 'products',
properties: {
// ...
},
// ...
});
Then it is possible to call:
import uranio from 'uranio';
const trx_response = await uranio.hooks.products.find_id('61dc3434a99090002c28cb4b');
if(trx_response.success){
const product = trx_response.payload;
}
Uranio TRX provides an authentication method for each AuthAtom.
See what is an AuthAtom
const auth_base = uranio.auth.create('user');
const trx_response = auth_base.authenticate('email@email.com', '[PASSWORD]');
if(trx_response.success){
const token = trx_response.payload.token;
}
If the authentication succeed, the server respond with a payload
containing a
JWT token
.
The JWT token
can be used in the other methods that need to be authenticated:
const trx_response = await uranio.hooks.products.find_id('61dc3434a99090002c28cb4b', {}, token)
if(trx_response.success){
const product = trx_response.payload;
}
It is also possible to set the token for all the hooks by using:
uranio.hooks.set_token(token);
const trx_response = await uranio.hooks.products.find_id('61dc3434a99090002c28cb4b');
if(trx_response.success){
const product = trx_response.payload;
}
If the authentication succeed the server will also send back a Set-Cookie
Header
with the JWT token
.
The cookie is HttpOnly; SameSite=Strict; Secure;
. Therefore the browser
will send back the token
for each request without JS needed.
But it will do only if the server is the same.
See HttpOnly flag
See SameSite flag
For any custom routes, Uranio TRX will provide additional hooks:
// src/atoms/product/routes/add_review.ts
import uranio from 'uranio';
export default uranio.register.route({
url: '/add-review-custom-url',
method: uranio.types.RouteMethod.POST,
action: uranio.types.AuthAction.WRITE,
query: ['stars', 'customer'],
return 'number',
call: async (request:uranio.types.Api.Request<'product','add_review'>):Promise<'number'>{
// Some logic
const bll_customers = uranio.core.bll.create('customer', request.passport);
const customer = await bll_customers.find_id(request.customer);
// Some logic
return request.query.stars || 0;
}
});
It will then be possible to call:
const trx_response = await uranio.hooks.products.add_review({query: {stars: 5}});
if(trx_response.success){
const product = trx_response.payload;
}