Client - Setup
Install Eicrud's client in your front-end to query your services.
EachCrudClient
is configured for one CrudService.
const { CrudClient, ClientConfig } = require('@eicrud/client')
const config: ClientConfig = {
serviceName: 'profile',
url: "https://<eicrud_app_host>"
}
const profileClient = new CrudClient(config)
export interface ClientConfig {
serviceName: string,
url: string,
onLogout?: () => void,
storage?: ClientStorage,
id_field?: string,
globalMockRole: string,
defaultBatchSize?: number,
cmdDefaultBatchMap?: { [key: string]: { batchField, batchSize: number} },
limitingFields: string[],
defaultProgressCallBack?:
(progress: number, total: number, type: 'limit' | 'batch') => Promise<void>,
};
Login
You can use the client as a guest or log in as a specific user.
const dto = {
email: "myuser@mail.com",
password: "p4ssw0rd",
expiresInSec: 60*30
}
await profileClient.login(dto);
Note
Make sure to allow the login
and check_jwt
commands in your user service security. See this example.
expiresInSec
Indicates how long until the authentication token expires.
Maximum expiresInSec
is specified in the maxJwtexpiresInSec
authentication option.
Info
If one of the clients encounters a 401
error, it will delete the JWT from storage, call the onLogout
callback and retry the request as a guest.
checkJwt
You can call checkJwt
to check if a user is currently logged in. It will extend the authentication duration if the renewJwt
option is set.
logout
It is possible to manually log out.
Dynamic client
You can make the client dynamic by putting it in a wrapper class.
import { ClientConfig, ClientStorage, CrudClient } from "@eicrud/client";
export class DynamicClient {
storage: ClientStorage;
config: ClientConfig;
constructor(conf: Partial<ClientConfig> = {}) {
this.config = {
url: 'http://localhost:3000',
serviceName: 'user',
...conf
}
const client = new CrudClient(this.config);
this.storage = client.config.storage;
}
get(serviceName){
return new CrudClient({...this.config, serviceName, storage: this.storage});
}
}
For an even better experience check out the super-client which comes with all your Entity/DTO types.
Note
If you need a client for other languages (Java, Python, Swift... etc.), check out the OpenAPI recipe that lets you export all your endpoints into an OpenAPI schema. From there you can use generators or build your own client.