/* * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ import { GustoEmbeddedCore } from "../core.js"; import { encodeJSON, encodeSimple } from "../lib/encodings.js"; import { matchStatusCode } from "../lib/http.js"; import * as M from "../lib/matchers.js"; import { compactMap } from "../lib/primitives.js"; import { safeParse } from "../lib/schemas.js"; import { RequestOptions } from "../lib/sdks.js"; import { extractSecurity, resolveGlobalSecurity } from "../lib/security.js"; import { pathToFunc } from "../lib/url.js"; import { GustoEmbeddedError } from "../models/errors/gustoembeddederror.js"; import { ConnectionError, InvalidRequestError, RequestAbortedError, RequestTimeoutError, UnexpectedClientError, } from "../models/errors/httpclienterrors.js"; import { NotFoundErrorObject, NotFoundErrorObject$inboundSchema, } from "../models/errors/notfounderrorobject.js"; import { ResponseValidationError } from "../models/errors/responsevalidationerror.js"; import { SDKValidationError } from "../models/errors/sdkvalidationerror.js"; import { UnprocessableEntityError, UnprocessableEntityError$inboundSchema, } from "../models/errors/unprocessableentityerror.js"; import { PostV1CompanyFlowsRequest, PostV1CompanyFlowsRequest$outboundSchema, PostV1CompanyFlowsResponse, PostV1CompanyFlowsResponse$inboundSchema, } from "../models/operations/postv1companyflows.js"; import { APICall, APIPromise } from "../types/async.js"; import { Result } from "../types/fp.js"; /** * Create a flow * * @remarks * Generate a link to access a pre-built workflow in Gusto white-label UI. For security, all generated flows will expire within 1 hour of inactivity or 24 hours from creation time, whichever comes first. * * You can see a list of all possible flow types in our [Flow Types](https://docs.gusto.com/embedded-payroll/docs/flow-types) guide. * * You can also mix and match flow_types in the same category to create custom flows suitable for your needs. * * For instance, to create a custom onboarding flow that only includes `add_addresses`, `add_employees`, and `sign_all_forms` steps, simply stitch those flow_types together into a comma delimited string: * * ```json * { * "flow_type": "add_addresses,add_employees,sign_all_forms" * } * ``` * * Please be mindful of data dependencies in each step to achieve the best user experience. * * For more information and in-depth guides review the [Getting Started](https://docs.gusto.com/embedded-payroll/docs/flows-getting-started) guide for flows. * * scope: `flows:write` * * If set, this operation will use {@link Security.companyAccessAuth} from the global security. */ export function flowsCreate( client: GustoEmbeddedCore, request: PostV1CompanyFlowsRequest, options?: RequestOptions, ): APIPromise< Result< PostV1CompanyFlowsResponse, | NotFoundErrorObject | UnprocessableEntityError | GustoEmbeddedError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError > > { return new APIPromise($do( client, request, options, )); } async function $do( client: GustoEmbeddedCore, request: PostV1CompanyFlowsRequest, options?: RequestOptions, ): Promise< [ Result< PostV1CompanyFlowsResponse, | NotFoundErrorObject | UnprocessableEntityError | GustoEmbeddedError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >, APICall, ] > { const parsed = safeParse( request, (value) => PostV1CompanyFlowsRequest$outboundSchema.parse(value), "Input validation failed", ); if (!parsed.ok) { return [parsed, { status: "invalid" }]; } const payload = parsed.value; const body = encodeJSON("body", payload["Create-Flow-Request"], { explode: true, }); const pathParams = { company_uuid: encodeSimple("company_uuid", payload.company_uuid, { explode: false, charEncoding: "percent", }), }; const path = pathToFunc("/v1/companies/{company_uuid}/flows")(pathParams); const headers = new Headers(compactMap({ "Content-Type": "application/json", Accept: "application/json", "X-Gusto-API-Version": encodeSimple( "X-Gusto-API-Version", payload["X-Gusto-API-Version"], { explode: false, charEncoding: "none" }, ), })); const secConfig = await extractSecurity(client._options.companyAccessAuth); const securityInput = secConfig == null ? {} : { companyAccessAuth: secConfig }; const requestSecurity = resolveGlobalSecurity(securityInput, [0]); const context = { options: client._options, baseURL: options?.serverURL ?? client._baseURL ?? "", operationID: "post-v1-company-flows", oAuth2Scopes: null, resolvedSecurity: requestSecurity, securitySource: client._options.companyAccessAuth, retryConfig: options?.retries || client._options.retryConfig || { strategy: "none" }, retryCodes: options?.retryCodes || ["429", "500", "502", "503", "504"], }; const requestRes = client._createRequest(context, { security: requestSecurity, method: "POST", baseURL: options?.serverURL, path: path, headers: headers, body: body, userAgent: client._options.userAgent, timeoutMs: options?.timeoutMs || client._options.timeoutMs || -1, }, options); if (!requestRes.ok) { return [requestRes, { status: "invalid" }]; } const req = requestRes.value; const doResult = await client._do(req, { context, isErrorStatusCode: (statusCode: number) => matchStatusCode({ status: statusCode } as Response, ["4XX", "5XX"]), retryConfig: context.retryConfig, retryCodes: context.retryCodes, }); if (!doResult.ok) { return [doResult, { status: "request-error", request: req }]; } const response = doResult.value; const responseFields = { HttpMeta: { Response: response, Request: req }, }; const [result] = await M.match< PostV1CompanyFlowsResponse, | NotFoundErrorObject | UnprocessableEntityError | GustoEmbeddedError | ResponseValidationError | ConnectionError | RequestAbortedError | RequestTimeoutError | InvalidRequestError | UnexpectedClientError | SDKValidationError >( M.json(201, PostV1CompanyFlowsResponse$inboundSchema, { key: "Flow" }), M.jsonErr(404, NotFoundErrorObject$inboundSchema), M.jsonErr(422, UnprocessableEntityError$inboundSchema), M.fail("4XX"), M.fail("5XX"), )(response, req, { extraFields: responseFields }); if (!result.ok) { return [result, { status: "complete", request: req, response }]; } return [result, { status: "complete", request: req, response }]; }