Web Server
Interaqt exports a startServer
function, allowing you to quickly create a server.
It defaults to using fastify as its underlying framework.
Applications created using npx create-interaqt-app can find relevant configurations in /server.ts
.
Initializing Data
Besides server.ts
, you can also find the script for initializing the database in /install.ts
.
This script can be executed with npm run install.
By default, it only creates the database and tables.
If you need to add initial data, you can modify the script directly.
Use the controller.system.storage.create
function to insert initial data.
Authentication
The second parameter of startServer
, named config, contains a parseUserId
configuration.
Here, you might access information about each request (standard fastify Request object),
where you can implement permission control for each request.
In the default code generated by create-interaqt-app, it reads x-user-id
as userId,
designed to let developers quickly simulate the current user in development/testing environments by passing parameters.
We recommend using a complete registration and security system, such as logto/auth0. Please refer to use logto as authentication system.
Custom APIs
Interaqt automatically creates routes with the same name for all Interactions.
For example, createRequest will create the route POST /interaction/createRequest
.
If you need to manually create routes, you can pass them as the third parameter.
We can use createDataAPI
to directly expose functions as APIs.
If the passed parameters implement the fromValue
and toValue
methods, they will be automatically serialized and deserialized.
Here's an example of directly exposing a record query interface,
where the BoolExp
class implements fromValue and toValue methods.
When calling from the client side, you can use BoolExp
to construct parameters and serialize them using its toValue
method when actually calling.
const apis = {
getRecords: createDataAPI(
function getRecords(
this: Controller,
context: DataAPIContext,
recordName:string,
match: BoolExp<any>,
attributes = ['*']
) {
return this.system.storage.find(recordName, match, undefined, attributes)
},
{ allowAnonymous: true, params: ['string', BoolExp, 'object'] }
),
}
startServer(controller, {
port: PORT,
parseUserId: async (headers: IncomingHttpHeaders) => {
return headers['x-user-id'] as string
}
}, apis)
In Custom API, Controller.system.storage
is often used to fetch or update data. For specific APIs, refer to reference/storage.