@aimarch/loom-feedback 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -18,10 +18,10 @@ const { data } = await feedback.list({ page: 1 }, options);
18
18
  | --- | --- | --- |
19
19
  | submit | kind、body、contact、consent、requestKey、可选 context | feedback:write |
20
20
  | list | page 或 id,空对象查询第一页 | feedback:read |
21
- | messages | 仅会话选项 | feedback:read |
21
+ | messages | 会话选项及可选 page,默认第1页 | feedback:read |
22
22
  | readMessage | id | feedback:read |
23
23
 
24
- submit/list/readMessage 的第二参数是会话选项;messages 仅接收会话选项。所有方法返回 `{ data, requestId }`。list 的 id 条件查询本人当前产品的反馈详情,找不到返回 NOT_FOUND。messages 返回 `{items}`,readMessage 返回 `{id}`,submit 返回 `{id,message}`。
24
+ submit/list/readMessage 的第二参数是会话选项;messages 接收会话选项及可选page。所有方法返回 `{ data, requestId }`。list 的 id 条件查询本人当前产品的反馈详情,找不到返回 NOT_FOUND。messages 返回 `{items,page,hasMore}`,readMessage 返回 `{id}`,submit 返回 `{id,message}`。
25
25
 
26
26
  反馈类型为“问题反馈”“功能建议”“体验优化”“其他意见”;正文去除首尾空白后1–500字符,联系方式最多100字符。requestKey为8–100位字母、数字、下划线或连字符,相同提交重试沿用同一键。context只接受page、courseId、classId;page为不含查询串及片段的站内路径。账号由会话确定,产品绑定客户端配置,服务端执行授权与数据隔离。
27
27
 
@@ -32,3 +32,17 @@ submit/list/readMessage 的第二参数是会话选项;messages 仅接收会
32
32
  仅从服务端模块导入,运行于浏览器时立即拒绝。接入方维护Cookie、Origin/CSRF检查和用户页面。反馈数据、处理结果及通知统一由Loom维护。读取结果仅投影公开字段。
33
33
 
34
34
  本地开发:`npm run build`,`npm pack`;发布后使用 `npm install @aimarch/loom-feedback`。
35
+
36
+ ## 0.2.0 通知分页(本地候选,未发布)
37
+
38
+ ```ts
39
+ const { data: notices } = await feedback.messages({ ...options, page: 1 });
40
+ // 页面显示 notices.items;hasMore=true 时允许加载下一页。
41
+ const next = notices.hasMore
42
+ ? await feedback.messages({ ...options, page: notices.page + 1 })
43
+ : null;
44
+ ```
45
+
46
+ 每页固定20条,page为1–100000的整数;按created_at、id倒序,超出末页返回空items及hasMore:false。保留已读通知,账号/产品/撤回过滤不变。不传page仍可调用,但现在只返回第一页,不再全量读取。页码分页不提供跨请求快照;翻页期间新增通知可能使位置移动,调用方合并展示时可按id去重,刷新从第一页开始。
47
+
48
+ 升级顺序:先部署支持分页的Loom API,再发布并升级SDK及上游翻页逻辑。0.1.0会忽略page/hasMore,不能用于完整历史通知展示或全量未读统计;0.2.0会拒绝没有分页字段的旧服务响应,避免误报为完整结果。本轮未修改或发布Teaching。
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { LoomFeedbackClientOptions, FeedbackInput, FeedbackQuery, UserCallOptions } from './types.js';
1
+ import type { LoomFeedbackClientOptions, FeedbackInput, FeedbackQuery, UserCallOptions, FeedbackMessagesOptions } from './types.js';
2
2
  export { LoomFeedbackError } from './transport.js';
3
3
  export type * from './types.js';
4
4
  export declare function createLoomFeedbackClient(options: LoomFeedbackClientOptions): {
@@ -7,9 +7,7 @@ export declare function createLoomFeedbackClient(options: LoomFeedbackClientOpti
7
7
  id: string;
8
8
  message: string;
9
9
  }>>;
10
- messages(opts: UserCallOptions): Promise<import("./types.js").FeedbackResult<{
11
- items: import("./types.js").FeedbackNotice[];
12
- }>>;
10
+ messages(opts: FeedbackMessagesOptions): Promise<import("./types.js").FeedbackResult<import("./types.js").FeedbackNoticePage>>;
13
11
  readMessage(input: {
14
12
  id: string;
15
13
  }, opts: UserCallOptions): Promise<import("./types.js").FeedbackResult<{
package/dist/index.js CHANGED
@@ -18,19 +18,23 @@ export function createLoomFeedbackClient(options) {
18
18
  }
19
19
  }
20
20
  function headers(opts) {
21
- if (!opts || typeof opts.sessionToken !== 'string' || !/^loom_as_[A-Za-z0-9_-]{43}$/.test(opts.sessionToken))
21
+ if (!opts ||
22
+ typeof opts.sessionToken !== 'string' ||
23
+ !/^loom_as_[A-Za-z0-9_-]{43}$/.test(opts.sessionToken))
22
24
  throw new LoomFeedbackError('INVALID_INPUT', 0);
23
25
  return { 'X-Loom-Session': opts.sessionToken };
24
26
  }
25
27
  return {
26
28
  async list(input, opts) {
27
- return call('GET', base + checked(() => schema.query(input)), v => schema.page(v, product), headers(opts), undefined, opts);
29
+ return call('GET', base + checked(() => schema.query(input)), (v) => schema.page(v, product), headers(opts), undefined, opts);
28
30
  },
29
31
  async submit(input, opts) {
30
32
  return call('POST', base, schema.submitted, headers(opts), checked(() => schema.input(input)), opts);
31
33
  },
32
34
  async messages(opts) {
33
- return call('GET', base + '/messages', schema.notices, headers(opts), undefined, opts);
35
+ const authHeaders = headers(opts);
36
+ const query = checked(() => schema.query({ page: opts.page }));
37
+ return call('GET', base + '/messages' + query, schema.notices, authHeaders, undefined, opts);
34
38
  },
35
39
  async readMessage(input, opts) {
36
40
  return call('POST', base + '/messages/read', schema.receipt, headers(opts), { id: checked(() => schema.id(input.id)) }, opts);
package/dist/schema.d.ts CHANGED
@@ -1,12 +1,10 @@
1
- import type { FeedbackInput, FeedbackNotice, FeedbackPage, ProductCode } from './types.js';
1
+ import type { FeedbackInput, FeedbackNoticePage, FeedbackPage, ProductCode } from './types.js';
2
2
  export declare const record: (v: unknown) => v is Record<string, unknown>;
3
3
  export declare const id: (v: unknown) => string;
4
4
  export declare function input(v: unknown): FeedbackInput;
5
5
  export declare function query(v: unknown): string;
6
6
  export declare function page(v: unknown, product: ProductCode): FeedbackPage;
7
- export declare function notices(v: unknown): {
8
- items: FeedbackNotice[];
9
- };
7
+ export declare function notices(v: unknown): FeedbackNoticePage;
10
8
  export declare function receipt(v: unknown): {
11
9
  id: string;
12
10
  };
package/dist/schema.js CHANGED
@@ -1,18 +1,28 @@
1
1
  export const record = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
2
- const fail = () => { throw new Error('Invalid feedback data'); };
3
- const obj = (v) => record(v) ? v : fail();
4
- const text = (v) => typeof v === 'string' ? v : fail();
5
- export const id = (v) => typeof v === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(v) ? v : fail();
2
+ const fail = () => {
3
+ throw new Error('Invalid feedback data');
4
+ };
5
+ const obj = (v) => (record(v) ? v : fail());
6
+ const text = (v) => (typeof v === 'string' ? v : fail());
7
+ export const id = (v) => typeof v === 'string' && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(v)
8
+ ? v
9
+ : fail();
6
10
  const date = (v) => typeof v === 'string' && Number.isFinite(Date.parse(v)) ? v : fail();
7
- const nullableDate = (v) => v === null ? null : date(v);
11
+ const nullableDate = (v) => (v === null ? null : date(v));
8
12
  const kind = (v) => ['问题反馈', '功能建议', '体验优化', '其他意见'].includes(text(v)) ? v : fail();
9
- const keys = (v, allowed) => { if (Object.keys(v).some(k => !allowed.includes(k)))
10
- fail(); };
13
+ const keys = (v, allowed) => {
14
+ if (Object.keys(v).some((k) => !allowed.includes(k)))
15
+ fail();
16
+ };
11
17
  export function input(v) {
12
18
  const o = obj(v);
13
19
  keys(o, ['kind', 'body', 'contact', 'consent', 'requestKey', 'context']);
14
20
  const body = text(o.body), contact = text(o.contact), requestKey = text(o.requestKey);
15
- if ([...body.trim()].length < 1 || [...body.trim()].length > 500 || [...contact].length > 100 || typeof o.consent !== 'boolean' || !/^[A-Za-z0-9_-]{8,100}$/.test(requestKey))
21
+ if ([...body.trim()].length < 1 ||
22
+ [...body.trim()].length > 500 ||
23
+ [...contact].length > 100 ||
24
+ typeof o.consent !== 'boolean' ||
25
+ !/^[A-Za-z0-9_-]{8,100}$/.test(requestKey))
16
26
  fail();
17
27
  let context;
18
28
  if (o.context !== undefined) {
@@ -30,7 +40,14 @@ export function input(v) {
30
40
  if (c.classId !== undefined)
31
41
  context.classId = id(c.classId);
32
42
  }
33
- return { kind: kind(o.kind), body, contact, consent: o.consent, requestKey, ...(context === undefined ? {} : { context }) };
43
+ return {
44
+ kind: kind(o.kind),
45
+ body,
46
+ contact,
47
+ consent: o.consent,
48
+ requestKey,
49
+ ...(context === undefined ? {} : { context }),
50
+ };
34
51
  }
35
52
  export function query(v) {
36
53
  const q = obj(v);
@@ -47,21 +64,58 @@ export function query(v) {
47
64
  }
48
65
  export function page(v, product) {
49
66
  const p = obj(v);
50
- if (!Array.isArray(p.items) || p.items.length > 20 || !Number.isInteger(p.page) || Number(p.page) < 1 || Number(p.page) > 100000 || typeof p.hasMore !== 'boolean')
67
+ if (!Array.isArray(p.items) ||
68
+ p.items.length > 20 ||
69
+ !Number.isInteger(p.page) ||
70
+ Number(p.page) < 1 ||
71
+ Number(p.page) > 100000 ||
72
+ typeof p.hasMore !== 'boolean')
51
73
  fail();
52
74
  const items = p.items.map((raw) => {
53
75
  const x = obj(raw);
54
76
  if (x.product_code !== product || !['submitted', 'handled'].includes(text(x.status)))
55
77
  fail();
56
- return { id: id(x.id), product_code: product, kind: kind(x.kind), body: text(x.body), status: x.status, reply: text(x.reply), created_at: date(x.created_at), handled_at: nullableDate(x.handled_at) };
78
+ return {
79
+ id: id(x.id),
80
+ product_code: product,
81
+ kind: kind(x.kind),
82
+ body: text(x.body),
83
+ status: x.status,
84
+ reply: text(x.reply),
85
+ created_at: date(x.created_at),
86
+ handled_at: nullableDate(x.handled_at),
87
+ };
57
88
  });
58
89
  return { items, page: p.page, hasMore: p.hasMore };
59
90
  }
60
91
  export function notices(v) {
61
92
  const o = obj(v);
62
- if (!Array.isArray(o.items))
93
+ if (!Array.isArray(o.items) ||
94
+ o.items.length > 20 ||
95
+ !Number.isInteger(o.page) ||
96
+ Number(o.page) < 1 ||
97
+ Number(o.page) > 100000 ||
98
+ typeof o.hasMore !== 'boolean')
63
99
  fail();
64
- return { items: o.items.map(raw => { const x = obj(raw); return { id: id(x.id), title: text(x.title), body: text(x.body), feedback_id: id(x.feedback_id), created_at: date(x.created_at), read_at: nullableDate(x.read_at) }; }) };
100
+ return {
101
+ items: o.items.map((raw) => {
102
+ const x = obj(raw);
103
+ return {
104
+ id: id(x.id),
105
+ title: text(x.title),
106
+ body: text(x.body),
107
+ feedback_id: id(x.feedback_id),
108
+ created_at: date(x.created_at),
109
+ read_at: nullableDate(x.read_at),
110
+ };
111
+ }),
112
+ page: o.page,
113
+ hasMore: o.hasMore,
114
+ };
115
+ }
116
+ export function receipt(v) {
117
+ return { id: id(obj(v).id) };
118
+ }
119
+ export function submitted(v) {
120
+ return { ...receipt(v), message: text(obj(v).message) };
65
121
  }
66
- export function receipt(v) { return { id: id(obj(v).id) }; }
67
- export function submitted(v) { return { ...receipt(v), message: text(obj(v).message) }; }
package/dist/types.d.ts CHANGED
@@ -35,6 +35,11 @@ export interface FeedbackNotice {
35
35
  created_at: string;
36
36
  read_at: string | null;
37
37
  }
38
+ export interface FeedbackNoticePage {
39
+ items: FeedbackNotice[];
40
+ page: number;
41
+ hasMore: boolean;
42
+ }
38
43
  export interface FeedbackQuery {
39
44
  page?: number;
40
45
  id?: string;
@@ -46,6 +51,9 @@ export interface CallOptions {
46
51
  export interface UserCallOptions extends CallOptions {
47
52
  sessionToken: string;
48
53
  }
54
+ export interface FeedbackMessagesOptions extends UserCallOptions {
55
+ page?: number;
56
+ }
49
57
  export interface FeedbackResult<T> {
50
58
  data: T;
51
59
  requestId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aimarch/loom-feedback",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Server-only product-scoped Loom feedback HTTP client",
5
5
  "type": "module",
6
6
  "files": [