0.1.1 • Published 2 months ago
@jasonai/client v0.1.1
- Build backend as function calls
- Export to APIClient and use as services in frontend; includes function calls, validations and authentication
Backend
- createServer: create express server function
- Must export default at src/calls.ts
- HttpCall: Generate APIClient: parameter doesn't work with property access (object.child)
- HttpCallValidation: Generate APIClient: parameter doesn't work with property access (object.child)
- HttpCallAuthentication: Generate APIClient: parameter doesn't work with property access (object.child)
- HttpCallCached: add response cache regardless input. Generate APIClient: parameter doesn't work with property access (object.child)
- HttpCallLimit: add rate limit. Generate APIClient: parameter doesn't work with property access (object.child)
- createHook: create hook and must use writeContext/readContext. use beforeRoute option to write data for use hook
- useAuth: auth hook, can use anywhere in the code
- useCache: access cache service
useData: access request data from params, query and body
TODO:
- example code: teach chatGpt and ask it generate docs and examples; also generate prompt to use next time
- TODO: make generate client quicker on dev, could just copy .ts files
APIClient Generation
- client
#Example
- src/calls.ts: create object as api endpoints structure. we'll have
/api/complete
and/api/messages
endpoints
import { HttpCall, HttpCallValidation } from "@jasonai/api";
export default {
complete: HttpCall(HttpCallValidation(CompleteRequestSchema), async (data: CompleteRequest): Promise<ChatMessageType> => {
return await chatManager.complete(data);
}),
messages: HttpCall(async () => {
return chatManager.messages;
})
}
- start server
import calls from './calls';
import { createServer } from '@jasonai/api';
const server = createServer({ calls });
server.start(3001);
- generate apiClient for the frontend to use. add this in package.json
"client:gen": "api generate-client --calls ./src/calls.ts --output ../web/src/client"
Front end project
- install
@jasonai/client
- build api script
npm run client:gen
from api project. client folder is created with apiClient - usage
import { createApiClient } from './client';
const apiClient = createApiClient({
host: 'http://localhost:3001'
});
const messages = await apiClient.client.messages();