JavaScript 関数の構築
JavaScript 関数の構造です。HTTP 関数とジョブ関数、型指定されたコントラクト、応答ヘルパー、エラー処理、およびログをカバーします。
すべての関数は、defineFunction(...) オブジェクトを既定でエクスポートする functions/ フォルダーに直接格納されるファイルです。関数の名前はファイル名です。名前がアンダースコアで始まるファイルはヘルパーであり、関数ではありません。CLI は、uipath.json 内のfunctions リストをフォルダーと同期させます。
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
export default defineFunction({
name: "process-order",
input: defineSchema<Input>(),
output: defineSchema<Output>(),
handler: async (input, ctx) => { /* ... */ },
});
import { defineFunction, defineSchema } from "@uipath/coded-functions-js-sdk";
export default defineFunction({
name: "process-order",
input: defineSchema<Input>(),
output: defineSchema<Output>(),
handler: async (input, ctx) => { /* ... */ },
});
HTTP とジョブ関数
uipath.json 内のプロジェクトのtargetRuntimeによって、プロジェクト内のすべての関数がどのように呼び出されるかが決まります。
targetRuntime | Functions | 宣言 | によって呼び出されます |
|---|---|---|---|
javascript-server | HTTP 関数 | method すべての機能に設定されpath | 関数の HTTP トリガーを介した、コード化されたアプリまたは任意の HTTP クライアント |
javascript | ジョブ機能 | method と path 省略 | Maestro サービス タスク、[ジョブを実行] アクティビティ、Orchestrator API、またはジョブ トリガー |
uip function new --kind http または --kind job targetRuntimeを設定します。HTTP 関数のプロジェクトで、methodとpathのない関数は、run、packserve失敗します。HTTP 関数とジョブ関数は、同じ方法でパッケージ化およびデプロイされます。
HTTP 関数は、コード化されたアプリをサポートします。ジョブ機能は、より大きなオートメーションのステップです。1 つ目の「 HTTP トリガーとルーティング 」、2 つ目の「 関数の呼び出し 」をご覧ください。
型指定されたコントラクト
defineSchema<T>() 、TypeScript インターフェイスを JSON スキーマに変換し、すべての呼び出しサーフェスで変数のバインドを駆動します。インターフェイスは信頼できる唯一の情報源であり、ハンドラーを型付けし、コントラクトを宣言します。
interface CreateOrderInput {
/** Customer reference. */
customerId: string;
/** @default 1 */
quantity?: number;
}
interface CreateOrderInput {
/** Customer reference. */
customerId: string;
/** @default 1 */
quantity?: number;
}
スキーマでは任意のプロパティが任意になり、JSDoc @default タグによって既定値が保持されます。JavaScript プロジェクトでは、 の代わりに JSON スキーマ オブジェクト リテラル defineSchema<T>()渡します。
スキーマをリテラルとして関数ファイルに書き込みます。変数を介して参照されるバインドされたオブジェクトまたはスキーマオブジェクトは、関数のコントラクトから除外できます。
output宣言は任意です。存在する場合、ハンドラーの戻り値は、関数を離れる前にそれに対して検証されます。
結果を返す
プレーンなオブジェクトを返して、そのオブジェクトを本体として送信する 200 。
handler: async (input) => ({ orderId: input.customerId, processed: true }),
handler: async (input) => ({ orderId: input.customerId, processed: true }),
それ以外の場合は、応答オブジェクトを返すか、ヘルパーを使用します。
import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";
return created({ id: "new-id" }); // 201
return notFound("No such order"); // 404
return { status: 202, body: { queued: true } };
import { ok, created, notFound } from "@uipath/coded-functions-js-sdk";
return created({ id: "new-id" }); // 201
return notFound("No such order"); // 404
return { status: 202, body: { queued: true } };
数値の status フィールドを持つ返されたオブジェクトは、応答として読み取られます。 status が HTTP ステータスになり、 body のみが送信されます。独自の出力フィールドに別の名前を付けます (例: httpStatus)。
Error
失敗する FunctionError をスローし、特定のステータスとメッセージが表示されます。
import { FunctionError } from "@uipath/coded-functions-js-sdk";
if (!input.customerId) {
throw new FunctionError("customerId is required", 400);
}
import { FunctionError } from "@uipath/coded-functions-js-sdk";
if (!input.customerId) {
throw new FunctionError("customerId is required", 400);
}
キャッチされないエラーは 500になります。関数をジョブとして実行する場合、スローされたエラーによってジョブがエラーになり、メッセージがジョブの結果に到達します。
ログ
SDK ロガーでログを記録します。そのエントリは、以下のレベルで Orchestrator のジョブ ログに到達します。
import { logger } from "@uipath/coded-functions-js-sdk";
logger.info(`Processed order ${input.orderId}`);
import { logger } from "@uipath/coded-functions-js-sdk";
logger.info(`Processed order ${input.orderId}`);
シークレット値はログに記録しないでください。
次のステップ
- HTTP トリガーとルーティング
- 関数コンテキスト — ID、プラットフォーム座標、要求データ
defineFunction参照 — すべてのフィールドとその既定値