UiPath Documentation
functions
latest
false
函数用户指南
  • 概述
    • 关于函数
  • JavaScript functions
    • 入门指南
    • Building JavaScript functions
    • HTTP triggers and routing
    • Function context
    • 访问平台服务
    • 测试和调试
  • Python 函数
  • 部署并运行
重要 :
请注意,此内容已使用机器翻译进行了本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。

Building JavaScript functions

Anatomy of a JavaScript function, covering the HTTP endpoint and job shapes, typed contracts, response helpers, error handling, and logging.

Every function is a module that default-exports a defineFunction(...) object. The CLI discovers it through the functions map in uipath.json.

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) => { /* ... */ },
});

The two shapes

A single field pair decides how a function is invoked.

ShapeDeclarationInvoked by
HTTP endpointmethod and path are setAn app or any HTTP client, through the function's trigger URL
作业method and path omittedA Maestro Service Task, a Run Job activity, the Orchestrator API, or a job trigger

Both shapes are packaged and deployed the same way. An HTTP function is what backs a Coded App; a job function is a step inside a larger automation. See HTTP triggers and routing for the first and Invoking functions for the second.

Typed contracts

defineSchema<T>() turns a TypeScript interface into the JSON Schema that drives variable binding on every invocation surface. The interface is the single source of truth — it types the handler and declares the contract:

interface CreateOrderInput {
  /** Customer reference. */
  customerId: string;
  /** @default 1 */
  quantity?: number;
}
interface CreateOrderInput {
  /** Customer reference. */
  customerId: string;
  /** @default 1 */
  quantity?: number;
}

Optional properties become optional in the schema, and a JSDoc @default tag carries the default value through. In JavaScript projects, pass a JSON Schema object literal in place of defineSchema<T>().

Schemas are extracted from the source without running it, so write them as literals. A value referenced through a variable — a numeric bound, or a shared schema object — can be dropped from the extracted schema, leaving the function with no contract to bind against.

Declaring output is optional. When present, the handler's return value is validated against it before it leaves the function.

Returning results

Return a plain object to send 200 with that object as the body:

handler: async (input) => ({ orderId: input.customerId, processed: true }),
handler: async (input) => ({ orderId: input.customerId, processed: true }),

For anything else, return a response object or use a helper:

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 } };
警告:

Because { status, body } is the response shape, an output field of your own named status holding a number is read as a status code. The declared status is sent with an empty body and the rest of your payload is dropped, with no error. Name the field something else, such as httpStatus.

错误

Throw FunctionError to fail with a specific status and message:

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);
}

An uncaught error becomes a 500. When the function runs as a job, a thrown error faults the job and the message reaches the job result.

日志记录

Use the SDK logger so output is attributed to the run and reaches Orchestrator job logs:

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}`);

console.* output is not forwarded. Secret values must never be logged.

后续步骤

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新