@atscript/vue-wf 0.1.163 → 0.1.165

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import { Fragment, computed, createCommentVNode, createElementBlock, createElementVNode, defineComponent, normalizeStyle, onMounted, onUnmounted, openBlock, ref, renderList, renderSlot, toDisplayString, watch } from "vue";
1
+ import { Fragment, computed, createCommentVNode, createElementBlock, createElementVNode, defineComponent, normalizeStyle, onMounted, onUnmounted, openBlock, ref, renderList, renderSlot, toDisplayString, unref, watch } from "vue";
2
+ import { useAsTexts } from "@atscript/vue-form/locale";
2
3
  //#region src/components/defaults/as-wf-finish.vue?vue&type=script&setup=true&lang.ts
3
4
  const _hoisted_1 = {
4
5
  key: 0,
@@ -62,6 +63,7 @@ var as_wf_finish_default = /* @__PURE__ */ defineComponent({
62
63
  }
63
64
  if (action.type === "dismiss") emit("dismiss");
64
65
  }
66
+ const { t } = useAsTexts();
65
67
  const next = computed(() => props.payload?.next ?? null);
66
68
  const message = computed(() => props.payload?.message ?? null);
67
69
  let autoTimer;
@@ -149,7 +151,7 @@ var as_wf_finish_default = /* @__PURE__ */ defineComponent({
149
151
  };
150
152
  });
151
153
  return (_ctx, _cache) => {
152
- return next.value?.trigger === "immediate" ? (openBlock(), createElementBlock("span", _hoisted_1, "Redirecting…")) : (openBlock(), createElementBlock("div", {
154
+ return next.value?.trigger === "immediate" ? (openBlock(), createElementBlock("span", _hoisted_1, toDisplayString(unref(t)("UI_WF_REDIRECTING")), 1)) : (openBlock(), createElementBlock("div", {
153
155
  key: 1,
154
156
  class: "as-wf-finish",
155
157
  onKeydown
@@ -175,7 +177,7 @@ var as_wf_finish_default = /* @__PURE__ */ defineComponent({
175
177
  totalSeconds: totalSeconds.value,
176
178
  skip: skipAuto,
177
179
  cancel: cancelAuto
178
- }, () => [createElementVNode("div", _hoisted_5, " Continuing in " + toDisplayString(secondsRemaining.value) + "… ", 1)])], 64)) : createCommentVNode("v-if", true),
180
+ }, () => [createElementVNode("div", _hoisted_5, toDisplayString(unref(t)("UI_WF_CONTINUING_IN", { seconds: secondsRemaining.value })), 1)])], 64)) : createCommentVNode("v-if", true),
179
181
  next.value?.trigger === "manual" ? (openBlock(), createElementBlock("div", _hoisted_6, [manualPrimary.value ? renderSlot(_ctx.$slots, "primary", {
180
182
  key: 0,
181
183
  button: manualPrimary.value,
@@ -1,2 +1,2 @@
1
- import { t as as_wf_finish_default } from "./as-wf-finish-CfbsKJhX.mjs";
1
+ import { t as as_wf_finish_default } from "./as-wf-finish-DSlzLuDT.mjs";
2
2
  export { as_wf_finish_default as default };
@@ -1,36 +1,47 @@
1
- import { t as as_wf_finish_default } from "./as-wf-finish-CfbsKJhX.mjs";
1
+ import { t as as_wf_finish_default } from "./as-wf-finish-DSlzLuDT.mjs";
2
2
  import { computed, createBlock, createCommentVNode, createElementBlock, createElementVNode, createSlots, createVNode, defineComponent, guardReactiveProps, normalizeProps, onMounted, onUnmounted, openBlock, reactive, ref, renderSlot, shallowRef, toDisplayString, toRaw, unref, watch, withCtx } from "vue";
3
3
  import { createFormData, createFormDef, getDeclaredFormActions } from "@atscript/ui";
4
+ import { AS_UI_MESSAGES } from "@atscript/ui/messages";
5
+ import { useAsTexts } from "@atscript/vue-form/locale";
4
6
  import { deserializeAnnotatedType } from "@atscript/typescript/utils";
5
7
  import { AsForm } from "@atscript/vue-form";
6
8
  //#region src/use-wf-form.ts
7
- const FRIENDLY_STATUS_MESSAGES = {
8
- 400: "The request was invalid. Please check your input and try again.",
9
- 401: "You need to sign in to continue.",
10
- 403: "You don't have permission to do that.",
11
- 404: "We couldn't find what you're looking for.",
12
- 408: "The request took too long. Please try again.",
13
- 409: "There's a conflict with the current state. Please refresh and try again.",
14
- 410: "This session has expired. Please start over.",
15
- 422: "Some of the information you provided couldn't be processed.",
16
- 423: "This account is temporarily locked. Please try again later.",
17
- 429: "Too many requests. Please wait a moment and try again.",
18
- 500: "Something went wrong on our end. Please try again in a moment.",
19
- 502: "We're having trouble reaching the server. Please try again shortly.",
20
- 503: "The service is temporarily unavailable. Please try again in a few minutes.",
21
- 504: "The server took too long to respond. Please try again."
22
- };
23
- function friendlyMessageForStatus(status) {
24
- const mapped = FRIENDLY_STATUS_MESSAGES[status];
25
- if (mapped) return mapped;
26
- if (status >= 500) return FRIENDLY_STATUS_MESSAGES[500];
27
- if (status >= 400) return "Something went wrong with that request. Please try again.";
28
- return "Unexpected response from the server.";
9
+ /**
10
+ * A status with its own friendly text (`UI_WF_STATUS_<status>`: client errors
11
+ * that map to specific user actions, server errors generic but human), else a
12
+ * generic one.
13
+ */
14
+ function friendlyMessageForStatus(status, t) {
15
+ const code = `UI_WF_STATUS_${status}`;
16
+ if (Object.hasOwn(AS_UI_MESSAGES, code)) return t(code);
17
+ if (status >= 500) return t("UI_WF_STATUS_500");
18
+ if (status >= 400) return t("UI_WF_REQUEST_FAILED");
19
+ return t("UI_WF_UNEXPECTED_RESPONSE");
20
+ }
21
+ /** The `$i18n` texts a form schema in an answer carries (`inputRequired.payload.$i18n`). */
22
+ function servedTexts(data) {
23
+ return (data?.inputRequired)?.payload?.$i18n;
24
+ }
25
+ /**
26
+ * A `{ error }` answer as a `WfFormError`: a string is the message; an
27
+ * object keeps its `message` / `status` / `code` and is the `body`.
28
+ */
29
+ function bodyError(raw, t) {
30
+ if (typeof raw === "string") return { message: raw };
31
+ const body = typeof raw === "object" && raw !== null ? raw : void 0;
32
+ const status = typeof body?.status === "number" ? body.status : void 0;
33
+ const failed = { message: typeof body?.message === "string" ? body.message : status !== void 0 ? friendlyMessageForStatus(status, t) : t("UI_WF_REQUEST_FAILED") };
34
+ if (status !== void 0) failed.status = status;
35
+ if (typeof body?.code === "string") failed.code = body.code;
36
+ if (body) failed.body = body;
37
+ return failed;
29
38
  }
30
39
  function useWfForm(options) {
31
40
  const tokenName = options.tokenName ?? "wfs";
32
41
  const wfidName = options.wfidName ?? "wfid";
33
42
  const transport = options.tokenTransport ?? "body";
43
+ const texts = useAsTexts();
44
+ const { t } = texts;
34
45
  const formDef = shallowRef(null);
35
46
  const formData = shallowRef(null);
36
47
  const formContext = shallowRef({});
@@ -47,12 +58,14 @@ function useWfForm(options) {
47
58
  let lastPayloadJson;
48
59
  let abortController;
49
60
  const { headers: extraHeaders, ...restFetchOpts } = options.fetchOptions ?? {};
61
+ const mergedHeaders = {
62
+ "Content-Type": "application/json",
63
+ ...extraHeaders instanceof Headers ? Object.fromEntries(extraHeaders) : Array.isArray(extraHeaders) ? Object.fromEntries(extraHeaders) : extraHeaders
64
+ };
65
+ const sendsLanguage = new Headers(mergedHeaders).has("accept-language");
50
66
  const baseFetchOpts = {
51
67
  method: "POST",
52
- headers: {
53
- "Content-Type": "application/json",
54
- ...extraHeaders instanceof Headers ? Object.fromEntries(extraHeaders) : Array.isArray(extraHeaders) ? Object.fromEntries(extraHeaders) : extraHeaders
55
- },
68
+ headers: mergedHeaders,
56
69
  ...transport === "cookie" ? { credentials: "include" } : {},
57
70
  ...restFetchOpts
58
71
  };
@@ -89,12 +102,12 @@ function useWfForm(options) {
89
102
  return;
90
103
  }
91
104
  if (data.error) {
92
- error.value = data.error;
105
+ error.value = bodyError(data.error, t);
93
106
  return;
94
107
  }
95
108
  const ir = data.inputRequired;
96
109
  if (!ir) {
97
- error.value = { message: "Unexpected response format" };
110
+ error.value = { message: t("UI_WF_UNEXPECTED_FORMAT") };
98
111
  return;
99
112
  }
100
113
  const { errors: serverErrors, ...remainingCtx } = ir.context ?? {};
@@ -118,30 +131,40 @@ function useWfForm(options) {
118
131
  loading.value = true;
119
132
  error.value = null;
120
133
  lastRequestBody = body;
134
+ const locale = texts.locale();
135
+ const headers = locale && !sendsLanguage ? {
136
+ ...mergedHeaders,
137
+ "Accept-Language": locale
138
+ } : mergedHeaders;
121
139
  try {
122
140
  const res = await (options.fetch ?? fetch)(options.path, {
123
141
  ...baseFetchOpts,
142
+ headers,
124
143
  signal,
125
144
  body: JSON.stringify(buildBody(body))
126
145
  });
127
146
  const data = await res.json().catch(() => null);
147
+ if (!sendsLanguage) texts.merge(locale, servedTexts(data));
128
148
  if (data && typeof data === "object" && !Array.isArray(data)) extractToken(data);
129
149
  if (!res.ok) {
130
150
  const errData = data;
131
- error.value = {
132
- message: errData?.message ?? errData?.error ?? friendlyMessageForStatus(res.status),
151
+ const failed = {
152
+ message: errData?.message ?? errData?.error ?? friendlyMessageForStatus(res.status, t),
133
153
  status: res.status
134
154
  };
155
+ if (typeof errData?.code === "string") failed.code = errData.code;
156
+ if (data !== null) failed.body = data;
157
+ error.value = failed;
135
158
  return;
136
159
  }
137
160
  if (!data) {
138
- error.value = { message: "Unexpected response format" };
161
+ error.value = { message: t("UI_WF_UNEXPECTED_FORMAT") };
139
162
  return;
140
163
  }
141
164
  processResponse(data);
142
165
  } catch (err) {
143
166
  if (err instanceof DOMException && err.name === "AbortError") return;
144
- error.value = { message: err instanceof Error ? err.message : "Network error" };
167
+ error.value = { message: err instanceof Error ? err.message : t("UI_WF_NETWORK_ERROR") };
145
168
  } finally {
146
169
  loading.value = false;
147
170
  }
@@ -296,6 +319,7 @@ var as_wf_form_default = /* @__PURE__ */ defineComponent({
296
319
  const props = __props;
297
320
  const emit = __emit;
298
321
  const wf = useWfForm(props);
322
+ const { t } = useAsTexts();
299
323
  watch(() => wf.loading.value, (v) => emit("loading", v));
300
324
  watch(() => wf.finished.value, (v) => {
301
325
  if (v) emit("finished", wf.response.value);
@@ -358,7 +382,7 @@ var as_wf_form_default = /* @__PURE__ */ defineComponent({
358
382
  })], -1))])])) : unref(wf).error.value && !unref(wf).formDef.value ? (openBlock(), createElementBlock("div", _hoisted_2, [renderSlot(_ctx.$slots, "wf.error", {
359
383
  error: unref(wf).error.value,
360
384
  retry: unref(wf).retry
361
- }, () => [createElementVNode("div", null, toDisplayString(unref(wf).error.value?.message ?? "Error"), 1)])])) : unref(wf).finished.value ? (openBlock(), createElementBlock("div", _hoisted_3, [renderSlot(_ctx.$slots, "wf.finished", {
385
+ }, () => [createElementVNode("div", null, toDisplayString(unref(wf).error.value?.message ?? unref(t)("UI_ERROR")), 1)])])) : unref(wf).finished.value ? (openBlock(), createElementBlock("div", _hoisted_3, [renderSlot(_ctx.$slots, "wf.finished", {
362
386
  response: unref(wf).response.value,
363
387
  payload: unref(wf).finishedPayload.value
364
388
  }, () => [createCommentVNode("\n Per `feedback_vue_empty_slot`: a `<template #x>` registered on a child\n short-circuits the child's default fallback even when its content is\n empty. So we only forward slot names that the AsWfForm consumer\n actually provided — otherwise AsWfFinish's defaults render.\n "), createVNode(as_wf_finish_default, {
@@ -396,7 +420,7 @@ var as_wf_form_default = /* @__PURE__ */ defineComponent({
396
420
  key: 0,
397
421
  error: unref(wf).error.value,
398
422
  retry: unref(wf).retry
399
- }, () => [createElementVNode("div", _hoisted_5, toDisplayString(unref(wf).error.value?.message ?? "Error"), 1)]) : createCommentVNode("v-if", true), (openBlock(), createBlock(unref(AsForm), {
423
+ }, () => [createElementVNode("div", _hoisted_5, toDisplayString(unref(wf).error.value?.message ?? unref(t)("UI_ERROR")), 1)]) : createCommentVNode("v-if", true), (openBlock(), createBlock(unref(AsForm), {
400
424
  key: unref(wf).formKey.value,
401
425
  def: unref(wf).formDef.value,
402
426
  "form-data": unref(wf).formData.value,
@@ -1,2 +1,2 @@
1
- import { t as _default } from "./as-wf-form.vue-ZXwNejxv.mjs";
1
+ import { t as _default } from "./as-wf-form.vue-C1mPDpdy.mjs";
2
2
  export { _default as default };
@@ -1,2 +1,2 @@
1
- import { t as as_wf_form_default } from "./as-wf-form-IIT8rtRM.mjs";
1
+ import { t as as_wf_form_default } from "./as-wf-form-ah0D5Nn1.mjs";
2
2
  export { as_wf_form_default as default };
@@ -11,7 +11,12 @@ interface UseWfFormOptions {
11
11
  path: string;
12
12
  /** Workflow ID to start (e.g., 'auth/login') */
13
13
  name: string;
14
- /** Initial input to send with the start request */
14
+ /**
15
+ * Initial form data for the start request. Sent as the first request's
16
+ * `input.formData` (the workflow protocol envelope), so the start step
17
+ * reads it with `useWfState().input().formData` — or validated, with
18
+ * unknown keys stripped, via `useAtscriptWf(Form).resolveInput()`.
19
+ */
15
20
  input?: Record<string, unknown>;
16
21
  /**
17
22
  * How to transport the workflow state token.
@@ -24,7 +29,11 @@ interface UseWfFormOptions {
24
29
  tokenName?: string;
25
30
  /** Workflow ID parameter name (default: 'wfid') */
26
31
  wfidName?: string;
27
- /** Custom fetch options (headers, credentials, etc.) */
32
+ /**
33
+ * Custom fetch options (headers, credentials, etc.). Requests carry an
34
+ * `Accept-Language` of the locale `provideAsLocale` gives, unless these
35
+ * headers set one.
36
+ */
28
37
  fetchOptions?: RequestInit;
29
38
  /**
30
39
  * Override the global `fetch` — lets the host wire its own status-code
@@ -43,6 +52,17 @@ interface UseWfFormOptions {
43
52
  */
44
53
  initialToken?: string;
45
54
  }
55
+ /** A failed round-trip, as `error` holds it (and `AsWfForm` emits it). */
56
+ interface WfFormError {
57
+ /** Text to show: the answer's `message` (or `error`), else a friendly text for the status. */
58
+ message: string;
59
+ /** HTTP status of a refused request (absent for a network error). */
60
+ status?: number;
61
+ /** The answer's machine-readable `code`, when it has one (e.g. a step's own refusal code). */
62
+ code?: string;
63
+ /** The parsed JSON body of a refused request (`code`, `params`, …), when it has one. */
64
+ body?: unknown;
65
+ }
46
66
  interface UseWfFormReturn {
47
67
  formDef: ShallowRef<FormDef | null>;
48
68
  formData: ShallowRef<Record<string, unknown> | null>;
@@ -327,26 +347,20 @@ declare const __VLS_base: vue.DefineComponent<AsWfFormProps, {
327
347
  action: typeof onAction;
328
348
  supportsAction: typeof supportsAction;
329
349
  }, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {} & {
350
+ dismiss: () => any;
351
+ action: (action: WfActionRequest) => any;
352
+ error: (error: WfFormError) => any;
330
353
  finished: (response: unknown) => any;
331
- error: (error: {
332
- message: string;
333
- status?: number;
334
- }) => any;
335
354
  submit: (data: unknown) => any;
336
355
  loading: (isLoading: boolean) => any;
337
- dismiss: () => any;
338
- action: (action: WfActionRequest) => any;
339
356
  form: (def: FormDef, context?: Record<string, unknown> | undefined) => any;
340
357
  }, string, vue.PublicProps, Readonly<AsWfFormProps> & Readonly<{
358
+ onDismiss?: (() => any) | undefined;
359
+ onAction?: ((action: WfActionRequest) => any) | undefined;
360
+ onError?: ((error: WfFormError) => any) | undefined;
341
361
  onFinished?: ((response: unknown) => any) | undefined;
342
- onError?: ((error: {
343
- message: string;
344
- status?: number;
345
- }) => any) | undefined;
346
362
  onSubmit?: ((data: unknown) => any) | undefined;
347
363
  onLoading?: ((isLoading: boolean) => any) | undefined;
348
- onDismiss?: (() => any) | undefined;
349
- onAction?: ((action: WfActionRequest) => any) | undefined;
350
364
  onForm?: ((def: FormDef, context?: Record<string, unknown> | undefined) => any) | undefined;
351
365
  }>, {
352
366
  tokenTransport: "body" | "cookie" | "query";
@@ -362,4 +376,4 @@ type __VLS_WithSlots<T, S> = T & {
362
376
  };
363
377
  };
364
378
  //#endregion
365
- export { useWfForm as i, UseWfFormOptions as n, UseWfFormReturn as r, _default as t };
379
+ export { useWfForm as a, WfFormError as i, UseWfFormOptions as n, UseWfFormReturn as r, _default as t };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
1
  import { a as WfNext, i as WfMessage, n as WfButton, r as WfFinished, t as WfActionRequest } from "./index-C5MmoWFB.mjs";
2
- import { i as useWfForm, n as UseWfFormOptions, r as UseWfFormReturn, t as _default$1 } from "./as-wf-form.vue-ZXwNejxv.mjs";
2
+ import { a as useWfForm, i as WfFormError, n as UseWfFormOptions, r as UseWfFormReturn, t as _default$1 } from "./as-wf-form.vue-C1mPDpdy.mjs";
3
3
  import { t as _default } from "./as-wf-finish.vue-BhXMooP3.mjs";
4
- export { _default as AsWfFinish, _default$1 as AsWfForm, type UseWfFormOptions, type UseWfFormReturn, type WfActionRequest, type WfButton, type WfFinished, type WfMessage, type WfNext, useWfForm };
4
+ export { _default as AsWfFinish, _default$1 as AsWfForm, type UseWfFormOptions, type UseWfFormReturn, type WfActionRequest, type WfButton, type WfFinished, type WfFormError, type WfMessage, type WfNext, useWfForm };
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as useWfForm, t as as_wf_form_default } from "./as-wf-form-IIT8rtRM.mjs";
2
- import { t as as_wf_finish_default } from "./as-wf-finish-CfbsKJhX.mjs";
1
+ import { n as useWfForm, t as as_wf_form_default } from "./as-wf-form-ah0D5Nn1.mjs";
2
+ import { t as as_wf_finish_default } from "./as-wf-finish-DSlzLuDT.mjs";
3
3
  export { as_wf_finish_default as AsWfFinish, as_wf_form_default as AsWfForm, useWfForm };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/vue-wf",
3
- "version": "0.1.163",
3
+ "version": "0.1.165",
4
4
  "description": "Workflow form integration for Vue 3 — HTTP round-trip loop driven by atscript type metadata",
5
5
  "keywords": [
6
6
  "atscript",
@@ -51,25 +51,25 @@
51
51
  "access": "public"
52
52
  },
53
53
  "dependencies": {
54
- "@atscript/ui": "^0.1.163"
54
+ "@atscript/ui": "^0.1.165"
55
55
  },
56
56
  "devDependencies": {
57
- "@atscript/core": "^0.2.3",
58
- "@atscript/typescript": "^0.2.3",
57
+ "@atscript/core": "^0.2.7",
58
+ "@atscript/typescript": "^0.2.7",
59
59
  "@vitejs/plugin-vue": "^6",
60
60
  "@vue/test-utils": "^2",
61
61
  "happy-dom": "^18",
62
- "unplugin-atscript": "^0.2.3",
62
+ "unplugin-atscript": "^0.2.7",
63
63
  "vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14",
64
64
  "vue": "^3",
65
65
  "vue-tsc": "~3.2.9",
66
- "@atscript/vue-form": "^0.1.163",
67
- "@atscript/moost-wf": "^0.1.163"
66
+ "@atscript/moost-wf": "^0.1.165",
67
+ "@atscript/vue-form": "^0.1.165"
68
68
  },
69
69
  "peerDependencies": {
70
- "@atscript/typescript": "^0.2.3",
70
+ "@atscript/typescript": "^0.2.7",
71
71
  "vue": "^3",
72
- "@atscript/vue-form": "^0.1.163"
72
+ "@atscript/vue-form": "^0.1.165"
73
73
  },
74
74
  "scripts": {
75
75
  "build": "node ../../scripts/gen-exports.mjs && vp pack",