Endpoint
定義
1 | import { App } from '@omujs/omu'; |
2 | import { EndpointType } from '@omujs/omu/api/endpoint'; |
3 |
|
4 | const APP = new App('com.example:service', { |
5 | type: 'service', |
6 | }); |
7 |
|
8 | const GREETING = EndpointType.createJson< |
9 | { name: string }, |
10 | { message: string } |
11 | >(APP, { |
12 | name: 'greeting', |
13 | }); |
処理を公開する
bind()
はomu.start()
より前に呼びます。
1 | omu.endpoints.bind(GREETING, async (request) => { |
2 | return { |
3 | message: `こんにちは、${request.name}さん`, |
4 | }; |
5 | }); |
呼び出す
1 | const response = await omu.endpoints.call(GREETING, { |
2 | name: 'Taro', |
3 | }); |
4 |
|
5 | console.log(response.message); |
呼び出しは既定で30秒経過するとタイムアウトします。処理時間に上限を
設けない場合はtimeout
にnull
を指定します。タイムアウトを無効にしても、
接続が切れた場合は呼び出しが失敗します。
1 | const response = await omu.endpoints.call(LONG_RUNNING_ENDPOINT, request, { |
2 | timeout: null, |
3 | }); |
権限を設定する
permissionId
を指定すると、呼び出し元に権限を要求できます。
1 | const PRIVATE_ENDPOINT = EndpointType.createJson(APP, { |
2 | name: 'private', |
3 | permissionId: WRITE_PERMISSION.id, |
4 | }); |
JSONで扱えない型はcreateSerialized()
とSerializer を使います。