@tunasand/sdk 0.6.0 → 0.8.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/CHANGELOG.md +116 -84
- package/README.md +209 -195
- package/dist/analytics.js +638 -2
- package/dist/app.js +230 -24
- package/dist/apps-BxnJ1RlU.js +435 -0
- package/dist/{bridge-DYAYGV7t.js → bridge-DtHw9hS6.js} +3 -3
- package/dist/bridge.js +2 -2
- package/dist/cli.js +5 -4
- package/dist/{model-pmXzGX12.js → collection-BEOxeISR.js} +49 -12
- package/dist/{customers-C_vJY2_v.js → customers-DTbL0qn6.js} +5 -3
- package/dist/customers.js +2 -2
- package/dist/{declared-C_GvPiGF.js → declared-DuNMsdXw.js} +93 -5
- package/dist/delivery.js +39 -8
- package/dist/embeds-B5V54Zur.js +34 -0
- package/dist/{errorLabels-BcyRbv_i.js → errorLabels-BgtQ_zUn.js} +277 -264
- package/dist/i18n.js +4 -4
- package/dist/ids--RF7hGyC.js +36 -0
- package/dist/integrations.js +4 -29
- package/dist/{ja-C1HEWWTQ.js → ja-CeHU0EaY.js} +3 -3
- package/dist/{ja-C6lX0CeJ.js → ja-Du6ooL6i.js} +12 -11
- package/dist/{page-E_cYgZx-.js → page-Bzrj24sJ.js} +4 -4
- package/dist/{profile-D6t1QMQh.js → profile-1em_lcxq.js} +5 -5
- package/dist/rich-text.d.ts +1 -0
- package/dist/{body.js → rich-text.js} +6 -7
- package/dist/{schema-UQJWRyfJ.js → schema-WnobuwLg.js} +136 -97
- package/dist/schema.js +346 -1
- package/dist/types/access/src/handoff.d.ts +3 -3
- package/dist/types/access/src/jwt.d.ts +1 -1
- package/dist/types/access/src/oidc.d.ts +1 -1
- package/dist/types/{cms-contract → contract}/src/apiKeys.d.ts +1 -1
- package/dist/types/{cms-contract → contract}/src/app.d.ts +8 -15
- package/dist/types/contract/src/appDefinition.d.ts +127 -0
- package/dist/types/{cms-contract → contract}/src/appDependencies.d.ts +6 -13
- package/dist/types/contract/src/appServer.d.ts +136 -0
- package/dist/types/contract/src/approvalLabels.d.ts +26 -0
- package/dist/types/contract/src/approvals.d.ts +63 -0
- package/dist/types/{cms-contract → contract}/src/apps.d.ts +72 -29
- package/dist/types/contract/src/auditActions.d.ts +18 -0
- package/dist/types/contract/src/auditLabels.d.ts +127 -0
- package/dist/types/{cms-contract → contract}/src/authorize.d.ts +22 -63
- package/dist/types/{cms-contract → contract}/src/bridge.d.ts +6 -6
- package/dist/types/{cms-contract → contract}/src/customers.d.ts +6 -6
- package/dist/types/{cms-contract → contract}/src/embeds.d.ts +13 -7
- package/dist/types/{cms-contract → contract}/src/errorLabels.d.ts +18 -16
- package/dist/types/{cms-contract → contract}/src/errorLabelsEn.d.ts +9 -7
- package/dist/types/{cms-contract → contract}/src/errors.d.ts +11 -11
- package/dist/types/{cms-contract → contract}/src/failureStatus.d.ts +4 -3
- package/dist/types/contract/src/fieldAccess.d.ts +45 -0
- package/dist/types/contract/src/fieldRules.d.ts +41 -0
- package/dist/types/{cms-contract/src/assets.d.ts → contract/src/files.d.ts} +5 -5
- package/dist/types/{cms-contract → contract}/src/functions.d.ts +3 -3
- package/dist/types/contract/src/hostingRpc.d.ts +143 -0
- package/dist/types/contract/src/hostnames.d.ts +30 -0
- package/dist/types/{cms-contract → contract}/src/identifiers.d.ts +31 -28
- package/dist/types/contract/src/ids.d.ts +104 -0
- package/dist/types/{cms-contract → contract}/src/import.d.ts +3 -3
- package/dist/types/contract/src/importLabels.d.ts +3 -0
- package/dist/types/contract/src/index.d.ts +67 -0
- package/dist/types/{cms-contract → contract}/src/issueLabels.d.ts +27 -23
- package/dist/types/{cms-contract → contract}/src/issueLabelsEn.d.ts +15 -13
- package/dist/types/contract/src/labels.d.ts +11 -0
- package/dist/types/contract/src/legacyAuditLabels.d.ts +10 -0
- package/dist/types/contract/src/livePeriods.d.ts +18 -0
- package/dist/types/contract/src/manage.d.ts +47 -0
- package/dist/types/contract/src/manageLabels.d.ts +9 -0
- package/dist/types/contract/src/mcpAuth.d.ts +139 -0
- package/dist/types/contract/src/mcpLabels.d.ts +5 -0
- package/dist/types/{cms-contract → contract}/src/migration.d.ts +1 -1
- package/dist/types/contract/src/operatorOperations.d.ts +703 -0
- package/dist/types/contract/src/operators.d.ts +33 -0
- package/dist/types/{cms-contract → contract}/src/paths.d.ts +6 -8
- package/dist/types/contract/src/pending.d.ts +4 -0
- package/dist/types/{cms-contract → contract}/src/permissions.d.ts +2 -0
- package/dist/types/contract/src/principal.d.ts +110 -0
- package/dist/types/{cms-contract → contract}/src/problems.d.ts +4 -4
- package/dist/types/contract/src/projectRole.d.ts +6 -0
- package/dist/types/{cms-contract → contract}/src/roles.d.ts +4 -3
- package/dist/types/{cms-contract → contract}/src/settingProblems.d.ts +2 -2
- package/dist/types/{cms-contract → contract}/src/settings.d.ts +2 -2
- package/dist/types/contract/src/settingsLabels.d.ts +3 -0
- package/dist/types/contract/src/slugs.d.ts +6 -0
- package/dist/types/{cms-contract → contract}/src/transfer.d.ts +3 -3
- package/dist/types/contract/src/trial.d.ts +10 -0
- package/dist/types/contract/src/versions.d.ts +9 -0
- package/dist/types/{cms-contract → contract}/src/webhooks.d.ts +33 -58
- package/dist/types/contract/src/workflow.d.ts +49 -0
- package/dist/types/contract/src/workspaceItems.d.ts +73 -0
- package/dist/types/contract/src/workspacePaths.d.ts +4 -0
- package/dist/types/{body → rich-text}/src/declared.d.ts +2 -2
- package/dist/types/{body → rich-text}/src/embed.d.ts +1 -1
- package/dist/types/{body → rich-text}/src/labels/ja.d.ts +2 -2
- package/dist/types/{body → rich-text}/src/profile.d.ts +1 -1
- package/dist/types/{model → schema}/src/audience.d.ts +11 -11
- package/dist/types/schema/src/brand.d.ts +5 -0
- package/dist/types/{model → schema}/src/capabilities.d.ts +5 -5
- package/dist/types/{model → schema}/src/changes.d.ts +6 -6
- package/dist/types/{model → schema}/src/check.d.ts +2 -2
- package/dist/types/{model/src/model.d.ts → schema/src/collection.d.ts} +25 -23
- package/dist/types/{platform/src/defaultModel.d.ts → schema/src/defaultSchema.d.ts} +4 -3
- package/dist/types/{model/src/asset.d.ts → schema/src/file.d.ts} +16 -16
- package/dist/types/{model → schema}/src/image.d.ts +3 -3
- package/dist/types/{model → schema}/src/index.d.ts +11 -10
- package/dist/types/{model → schema}/src/input.d.ts +3 -3
- package/dist/types/{model → schema}/src/integrity.d.ts +1 -1
- package/dist/types/{model → schema}/src/labels/en.d.ts +8 -7
- package/dist/types/{model → schema}/src/labels/ja.d.ts +9 -8
- package/dist/types/schema/src/legacy.d.ts +2 -0
- package/dist/types/{model → schema}/src/media.d.ts +1 -1
- package/dist/types/{model → schema}/src/mediaUrl.d.ts +5 -5
- package/dist/types/{model → schema}/src/page.d.ts +2 -2
- package/dist/types/{model → schema}/src/problems.d.ts +14 -13
- package/dist/types/{model → schema}/src/refs.d.ts +1 -1
- package/dist/types/{model → schema}/src/rules.d.ts +2 -2
- package/dist/types/{model → schema}/src/schema.d.ts +10 -10
- package/dist/types/{model → schema}/src/selfOps.d.ts +1 -1
- package/dist/types/schema/src/typesSource.d.ts +16 -0
- package/dist/types/{model → schema}/src/values.d.ts +2 -2
- package/dist/types/sdk/src/analytics.d.ts +2 -2
- package/dist/types/sdk/src/app.d.ts +44 -16
- package/dist/types/sdk/src/bridge.d.ts +12 -12
- package/dist/types/sdk/src/customers.d.ts +14 -20
- package/dist/types/sdk/src/delegation.d.ts +39 -0
- package/dist/types/sdk/src/delivery/client.d.ts +11 -10
- package/dist/types/sdk/src/delivery/fields.d.ts +2 -2
- package/dist/types/sdk/src/delivery/index.d.ts +2 -2
- package/dist/types/sdk/src/delivery/media.d.ts +17 -3
- package/dist/types/sdk/src/delivery/query.d.ts +1 -1
- package/dist/types/sdk/src/delivery/schema.d.ts +4 -4
- package/dist/types/sdk/src/delivery/values.d.ts +6 -6
- package/dist/types/sdk/src/delivery.d.ts +3 -3
- package/dist/types/sdk/src/i18n.d.ts +11 -11
- package/dist/types/sdk/src/integrations.d.ts +10 -10
- package/dist/types/sdk/src/{body.d.ts → rich-text.d.ts} +2 -2
- package/dist/types/sdk/src/schema.d.ts +5 -21
- package/dist/types/sdk/src/site.d.ts +1 -1
- package/dist/types/sdk/src/types.d.ts +21 -0
- package/dist/{schema-BPHL7Qr-.js → types-B77qvqPJ.js} +5 -4
- package/dist/types.d.ts +1 -0
- package/dist/types.js +1 -0
- package/dist/{typesSource-bO0jFw1I.js → typesSource-CWybQYus.js} +58 -12
- package/package.json +12 -12
- package/dist/apps-9u4XMi7E.js +0 -114
- package/dist/body.d.ts +0 -1
- package/dist/embed-R-OvDYAw.js +0 -90
- package/dist/model.d.ts +0 -1
- package/dist/model.js +0 -345
- package/dist/types/analytics/src/index.d.ts +0 -5
- package/dist/types/analytics/src/schedule.d.ts +0 -2
- package/dist/types/analytics/src/shared.d.ts +0 -21
- package/dist/types/analytics/src/source.d.ts +0 -19
- package/dist/types/analytics/src/store.d.ts +0 -24
- package/dist/types/model/src/brand.d.ts +0 -25
- package/dist/types/model/src/legacy.d.ts +0 -2
- package/dist/types/model/src/typesSource.d.ts +0 -8
- package/dist/types/sdk/src/model.d.ts +0 -5
- /package/dist/types/{cms-contract → contract}/src/appProblems.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/consents.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/customerLabels.d.ts +0 -0
- /package/dist/types/{analytics → contract}/src/day.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/functionLabels.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/permissionLabels.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/siteArtifact.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/siteDeclaration.d.ts +0 -0
- /package/dist/types/{analytics/src/view.d.ts → contract/src/viewEvent.d.ts} +0 -0
- /package/dist/types/{cms-contract → contract}/src/views.d.ts +0 -0
- /package/dist/types/{cms-contract → contract}/src/webhookLabels.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/components.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/derive.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/description.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/href.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/html.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/index.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/inline.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/labels/en.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/library.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/markdown.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/normalize.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/problems.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/render.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/schema.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/standard.d.ts +0 -0
- /package/dist/types/{body → rich-text}/src/types.d.ts +0 -0
- /package/dist/types/{model → schema}/src/contact.d.ts +0 -0
- /package/dist/types/{model → schema}/src/json.d.ts +0 -0
- /package/dist/types/{model → schema}/src/screens.d.ts +0 -0
- /package/dist/types/{model → schema}/src/time.d.ts +0 -0
package/dist/app.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { a as
|
|
6
|
-
import {
|
|
1
|
+
import { _ as JSON_BODY_MAX, d as problemEnglish, g as ERROR_STATUS, h as apiIssueText, i as errorText, n as errorBodyFor } from "./errorLabels-BgtQ_zUn.js";
|
|
2
|
+
import { t as newUuidV7 } from "./ids--RF7hGyC.js";
|
|
3
|
+
import { c as formatMessage, d as problemOf, f as problemText } from "./ja-CeHU0EaY.js";
|
|
4
|
+
import { o as TOKEN_EXCHANGE } from "./embeds-B5V54Zur.js";
|
|
5
|
+
import { a as accountDelegationOf, i as ownsRow, n as isAdmin, o as customerOf, r as allows, s as principalOfUser } from "./apps-BxnJ1RlU.js";
|
|
6
|
+
import { S as isPublishable, b as isPersonalCollection, j as roleFieldOf, l as audienceOf, t as AUDIENCES } from "./collection-BEOxeISR.js";
|
|
7
|
+
import { a as CUSTOMER_STATUSES, t as CUSTOMERS_KEY_TARGET } from "./customers-DTbL0qn6.js";
|
|
8
|
+
import { l as bodyProblemText, s as embedOfUrl } from "./declared-DuNMsdXw.js";
|
|
9
|
+
import { a as mergeStyle, c as ALIGNS, n as isSafeCssValue, o as sortMarks, r as HTML_TAG_MARKS, s as isSafeHref, u as isTableCell } from "./profile-1em_lcxq.js";
|
|
7
10
|
import { parseDocument } from "htmlparser2";
|
|
8
|
-
// ../
|
|
11
|
+
// ../rich-text/src/normalize.ts
|
|
9
12
|
const keyOf = (inline) => "component" in inline ? null : JSON.stringify([
|
|
10
13
|
sortMarks(inline.marks),
|
|
11
14
|
inline.link ?? null,
|
|
@@ -73,7 +76,7 @@ function normalizeBody(body) {
|
|
|
73
76
|
}
|
|
74
77
|
});
|
|
75
78
|
}
|
|
76
|
-
// ../
|
|
79
|
+
// ../rich-text/src/html.ts
|
|
77
80
|
/** 内容ごと捨てる要素 */
|
|
78
81
|
const DANGEROUS = /* @__PURE__ */ new Set([
|
|
79
82
|
"script",
|
|
@@ -721,10 +724,21 @@ function bodyFromHtml(html, options) {
|
|
|
721
724
|
}))
|
|
722
725
|
};
|
|
723
726
|
}
|
|
724
|
-
// ../
|
|
725
|
-
/**
|
|
726
|
-
|
|
727
|
-
|
|
727
|
+
// ../contract/src/identifiers.ts
|
|
728
|
+
/**
|
|
729
|
+
* インストールのトークンの形(GitHub App の形: アプリの鍵で署名したアプリの JWT と交換する)
|
|
730
|
+
* - JWS のコンパクトな形。発行と検証はどちらも Tunasand だけで、署名の鍵は SECRETS_KEY からプロジェクトごとに HKDF で導く
|
|
731
|
+
* - アプリの JWT: ES256 か RS256、iss はアプリの ID、aud はプロジェクトの配信の基点、exp − iat は appJwtMaxSec 秒まで
|
|
732
|
+
* - 時計のずれ: iat の未来と exp の過去を skewSec 秒まで許す
|
|
733
|
+
*/
|
|
734
|
+
const INSTALLATION_TOKEN = {
|
|
735
|
+
typ: "tunasand-installation+jwt",
|
|
736
|
+
alg: "HS256",
|
|
737
|
+
ttlSec: 3600,
|
|
738
|
+
appJwtMaxSec: 600,
|
|
739
|
+
skewSec: 60
|
|
740
|
+
};
|
|
741
|
+
// ../contract/src/appProblems.ts
|
|
728
742
|
/** 問題の表から、問題を作る関数(差し込みの不足は型の誤り) */
|
|
729
743
|
function appProblems(tables) {
|
|
730
744
|
const of = (code, params) => ({
|
|
@@ -746,7 +760,71 @@ function appProblems(tables) {
|
|
|
746
760
|
}
|
|
747
761
|
};
|
|
748
762
|
}
|
|
749
|
-
// ../
|
|
763
|
+
// ../contract/src/settingProblems.ts
|
|
764
|
+
/** アプリの設定値の問題(key は設定値のキー、params は文に差し込む値) */
|
|
765
|
+
var SettingProblem = class extends Error {
|
|
766
|
+
key;
|
|
767
|
+
problem;
|
|
768
|
+
params;
|
|
769
|
+
constructor(key, problem, params = {}) {
|
|
770
|
+
super(`app setting ${key}: ${problem}${Object.keys(params).length ? ` ${JSON.stringify(params)}` : ""}`);
|
|
771
|
+
this.name = "SettingProblem";
|
|
772
|
+
this.key = key;
|
|
773
|
+
this.problem = problem;
|
|
774
|
+
this.params = params;
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
// ../contract/src/workspaceItems.ts
|
|
778
|
+
/** 日付のフィールドの予定が持つ source(`field:<コレクション>.<フィールド>`) */
|
|
779
|
+
const calendarFieldSource = (f) => `field:${f.collection}.${f.field}`;
|
|
780
|
+
// ../contract/src/appDefinition.ts
|
|
781
|
+
/** 設定値のないアプリの設定値の解釈 */
|
|
782
|
+
const NO_SETTINGS = { parse: () => ({}) };
|
|
783
|
+
// ../contract/src/fieldAccess.ts
|
|
784
|
+
const ALL = /* @__PURE__ */ new Set([
|
|
785
|
+
"public",
|
|
786
|
+
"granted",
|
|
787
|
+
"self",
|
|
788
|
+
"staff"
|
|
789
|
+
]);
|
|
790
|
+
/** 配信 API に出せる見える範囲(公開とアクセス権、* の範囲) */
|
|
791
|
+
const DELIVERED = /* @__PURE__ */ new Set(["public", "granted"]);
|
|
792
|
+
/** MCP にスタッフの項目を出すコレクションの見える範囲(本人の項目は除く) */
|
|
793
|
+
const STAFF_FOR_MCP = /* @__PURE__ */ new Set([
|
|
794
|
+
"public",
|
|
795
|
+
"granted",
|
|
796
|
+
"staff"
|
|
797
|
+
]);
|
|
798
|
+
/** 能力のうちコレクション target を名指しした操作(* を除く) */
|
|
799
|
+
const namedOps = (capabilities, target) => Object.hasOwn(capabilities, target) && target !== "*" ? capabilities[target] : [];
|
|
800
|
+
/** 能力の操作(名指しと * の和) */
|
|
801
|
+
const opsOf = (capabilities, target) => [...namedOps(capabilities, target), ...Object.hasOwn(capabilities, "*") ? capabilities["*"] : []];
|
|
802
|
+
const READS = ["read", "read_drafts"];
|
|
803
|
+
/** 主体がコレクションのレコードで見られる見える範囲 */
|
|
804
|
+
function audiencesFor(reader, collection) {
|
|
805
|
+
if (reader === "system") return ALL;
|
|
806
|
+
if (reader.effective.form === "roles") {
|
|
807
|
+
const delegation = accountDelegationOf(reader);
|
|
808
|
+
if (!delegation) return ALL;
|
|
809
|
+
if (delegation.kind === "app") return namedOps(delegation.consented, collection.id).some((op) => READS.includes(op)) ? ALL : DELIVERED;
|
|
810
|
+
const { permissions } = reader.effective;
|
|
811
|
+
return (permissions[collection.id]?.read ?? permissions["*"]?.read ?? "none") !== "none" && !isPersonalCollection(collection) && delegation.staffCollections.includes(collection.id) ? STAFF_FOR_MCP : DELIVERED;
|
|
812
|
+
}
|
|
813
|
+
const { capabilities } = reader.effective;
|
|
814
|
+
if (!customerOf(reader) && namedOps(capabilities, collection.id).some((op) => READS.includes(op))) return ALL;
|
|
815
|
+
const out = /* @__PURE__ */ new Set(["public"]);
|
|
816
|
+
if (opsOf(capabilities, collection.id).includes("read_customers")) out.add("granted");
|
|
817
|
+
return out;
|
|
818
|
+
}
|
|
819
|
+
/** コレクションの最上位の項目を見られるか(読み替えは schema のパッケージの audienceOf) */
|
|
820
|
+
const canReadField = (audiences, field, collection) => audiences.has(audienceOf(field, collection));
|
|
821
|
+
/** レコードの題(題の項目を見られなければレコードの ID、題の項目がなければ ID) */
|
|
822
|
+
function titleFor(audiences, collection, values, id) {
|
|
823
|
+
const field = roleFieldOf(collection, "title");
|
|
824
|
+
const value = field && canReadField(audiences, field, collection) ? values[field.key] : null;
|
|
825
|
+
return typeof value === "string" && value ? value : id;
|
|
826
|
+
}
|
|
827
|
+
// ../contract/src/import.ts
|
|
750
828
|
/** 1回のリクエストで取り込む件数の上限(画像の取得の時間と、本文の大きさの上限 JSON_BODY_MAX のため) */
|
|
751
829
|
const IMPORT_ITEMS_MAX = 50;
|
|
752
830
|
/** 参照の項目の値で、同じ取り出し元の元の ID を指す形の印({ "$origin": "<元の ID>" }) */
|
|
@@ -796,6 +874,91 @@ function cyclicCollections(apis) {
|
|
|
796
874
|
});
|
|
797
875
|
return new Set(apis.filter((a) => reaches(a, a.endpoint, /* @__PURE__ */ new Set())).map((a) => a.endpoint));
|
|
798
876
|
}
|
|
877
|
+
// ../contract/src/app.ts
|
|
878
|
+
/** 外への読み込みの宛先の宣言に URL が当たるか(宣言は https のオリジン、ホスト名の先頭の `*.` は1つ以上の区間。https だけ、ポートの指定なし) */
|
|
879
|
+
function egressAllows(declared, url) {
|
|
880
|
+
if (url.protocol !== "https:" || url.port !== "") return false;
|
|
881
|
+
return declared.some((d) => {
|
|
882
|
+
const m = /^https:\/\/(\*\.)?([a-z0-9.-]+)$/.exec(d);
|
|
883
|
+
if (!m) return false;
|
|
884
|
+
return m[1] ? url.hostname.endsWith(`.${m[2]}`) : url.hostname === m[2];
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
// ../contract/src/workspacePaths.ts
|
|
888
|
+
/** ワークスペースのコレクションの経路(URL はレコードのスラッグ、record を省くと一覧) */
|
|
889
|
+
const workspaceRecordPath = (collection, record) => `/collections/${encodeURIComponent(collection.slug)}/records${record === void 0 ? "" : `/${encodeURIComponent(String(record.slug))}`}`;
|
|
890
|
+
// src/delegation.ts
|
|
891
|
+
/** アプリの JWT の有効の秒数(INSTALLATION_TOKEN.appJwtMaxSec の内) */
|
|
892
|
+
const ASSERTION_TTL_SEC = 60;
|
|
893
|
+
const base64Url = (bytes) => btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
894
|
+
const textPart = (value) => base64Url(new TextEncoder().encode(JSON.stringify(value)));
|
|
895
|
+
/** アプリの JWT(iss はアプリの ID、aud は環境の CMS の URL) */
|
|
896
|
+
async function appAssertion(key, claims, nowMs = Date.now()) {
|
|
897
|
+
const { kid, ...jwk } = key.jwk;
|
|
898
|
+
const signer = await crypto.subtle.importKey("jwk", {
|
|
899
|
+
...jwk,
|
|
900
|
+
key_ops: ["sign"]
|
|
901
|
+
}, {
|
|
902
|
+
name: "ECDSA",
|
|
903
|
+
namedCurve: "P-256"
|
|
904
|
+
}, false, ["sign"]);
|
|
905
|
+
const iat = Math.floor(nowMs / 1e3);
|
|
906
|
+
const input = `${textPart({
|
|
907
|
+
alg: "ES256",
|
|
908
|
+
typ: "JWT",
|
|
909
|
+
...kid && { kid }
|
|
910
|
+
})}.${textPart({
|
|
911
|
+
iss: claims.appId,
|
|
912
|
+
aud: claims.audience,
|
|
913
|
+
iat,
|
|
914
|
+
exp: iat + Math.min(ASSERTION_TTL_SEC, INSTALLATION_TOKEN.appJwtMaxSec)
|
|
915
|
+
})}`;
|
|
916
|
+
const signature = new Uint8Array(await crypto.subtle.sign({
|
|
917
|
+
name: "ECDSA",
|
|
918
|
+
hash: "SHA-256"
|
|
919
|
+
}, signer, new TextEncoder().encode(input)));
|
|
920
|
+
return `${input}.${base64Url(signature)}`;
|
|
921
|
+
}
|
|
922
|
+
/** 代理の委任の主張をアカウントの資格のトークンに替える */
|
|
923
|
+
async function exchangeDelegation(input) {
|
|
924
|
+
const send = input.fetch ?? ((request) => fetch(request));
|
|
925
|
+
const body = new URLSearchParams({
|
|
926
|
+
grant_type: TOKEN_EXCHANGE.grantType,
|
|
927
|
+
subject_token: input.delegation,
|
|
928
|
+
subject_token_type: TOKEN_EXCHANGE.subjectTokenType,
|
|
929
|
+
client_assertion_type: TOKEN_EXCHANGE.clientAssertionType,
|
|
930
|
+
client_assertion: await appAssertion(input.key, {
|
|
931
|
+
appId: input.appId,
|
|
932
|
+
audience: input.base
|
|
933
|
+
})
|
|
934
|
+
});
|
|
935
|
+
const res = await send(new Request(`${input.base}/api/embeds/token`, {
|
|
936
|
+
method: "POST",
|
|
937
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
938
|
+
body: body.toString()
|
|
939
|
+
}));
|
|
940
|
+
const parsed = await res.json().catch(() => null);
|
|
941
|
+
if (res.ok && typeof parsed?.access_token === "string") return {
|
|
942
|
+
ok: true,
|
|
943
|
+
token: parsed.access_token,
|
|
944
|
+
expiresIn: Number(parsed.expires_in ?? 0)
|
|
945
|
+
};
|
|
946
|
+
return {
|
|
947
|
+
ok: false,
|
|
948
|
+
error: typeof parsed?.error === "string" ? parsed.error : "upstream",
|
|
949
|
+
status: res.status
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
/** アカウントの資格のトークンで管理 API を呼ぶクライアント(path は /api の下、base は環境の CMS の URL) */
|
|
953
|
+
const bearerClient = (input) => ({ fetch: (path, init = {}) => {
|
|
954
|
+
const headers = new Headers(init.headers);
|
|
955
|
+
headers.set("authorization", `Bearer ${input.token}`);
|
|
956
|
+
const request = new Request(`${input.base}${path}`, {
|
|
957
|
+
...init,
|
|
958
|
+
headers
|
|
959
|
+
});
|
|
960
|
+
return input.fetch ? input.fetch(request) : fetch(request);
|
|
961
|
+
} });
|
|
799
962
|
// src/app.ts
|
|
800
963
|
/** 相関の ID のヘッダー(CMS の応答とログと同じ名前) */
|
|
801
964
|
const REQUEST_ID_HEADER = "x-request-id";
|
|
@@ -810,16 +973,59 @@ const appIssue = (i) => ({
|
|
|
810
973
|
severity: i.severity ?? "error"
|
|
811
974
|
});
|
|
812
975
|
/** エラーの応答(CMS の API と同じ形、相関の ID は要求の x-request-id。extra は issues などの足し) */
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
return
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
976
|
+
const appFailure = appFailureOf({});
|
|
977
|
+
/** エラーの応答を作る関数(targets はアプリの対象の英語の名前、キーは対象の id。message の対象の名前に使う) */
|
|
978
|
+
function appFailureOf(targets) {
|
|
979
|
+
return (request, code, params, extra = {}) => {
|
|
980
|
+
const given = request.headers.get(REQUEST_ID_HEADER);
|
|
981
|
+
const requestId = given && REQUEST_ID_FORM.test(given) ? given : newUuidV7();
|
|
982
|
+
return Response.json({
|
|
983
|
+
...errorBodyFor(code, requestId, params),
|
|
984
|
+
message: errorText(code, params, targets),
|
|
985
|
+
...extra,
|
|
986
|
+
requestId
|
|
987
|
+
}, {
|
|
988
|
+
status: ERROR_STATUS[code],
|
|
989
|
+
headers: { [REQUEST_ID_HEADER]: requestId }
|
|
990
|
+
});
|
|
991
|
+
};
|
|
992
|
+
}
|
|
993
|
+
/** 本文の JSON(空の本文は empty、壊れた JSON は400の応答) */
|
|
994
|
+
async function appJson(request, empty = null) {
|
|
995
|
+
const text = await request.text();
|
|
996
|
+
if (!text.trim()) return empty;
|
|
997
|
+
try {
|
|
998
|
+
return JSON.parse(text);
|
|
999
|
+
} catch {
|
|
1000
|
+
return appFailure(request, "invalid_input", void 0, { issues: [appIssue({
|
|
1001
|
+
path: "",
|
|
1002
|
+
message: apiIssueText("body_not_json")
|
|
1003
|
+
})] });
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
/** 入力の誤りの応答(zod の問題を CMS の API と同じ issues の形に。problems はアプリの英語の問題の文、キーは問題のコード) */
|
|
1007
|
+
function appInvalid(request, error, problems = {}) {
|
|
1008
|
+
const issues = error.issues.map((i) => {
|
|
1009
|
+
const path = i.path.join(".");
|
|
1010
|
+
const p = problemOf(i.message);
|
|
1011
|
+
if (!p) return appIssue({
|
|
1012
|
+
path,
|
|
1013
|
+
code: i.code,
|
|
1014
|
+
message: i.message
|
|
1015
|
+
});
|
|
1016
|
+
return appIssue({
|
|
1017
|
+
path,
|
|
1018
|
+
code: p.code,
|
|
1019
|
+
message: problemEnglish(i.message, problems),
|
|
1020
|
+
...Object.keys(p.params).length > 0 && { params: p.params }
|
|
1021
|
+
});
|
|
823
1022
|
});
|
|
1023
|
+
return appFailure(request, "invalid_input", void 0, { issues });
|
|
1024
|
+
}
|
|
1025
|
+
/** 呼んだ人のアカウント(管理の API の /api/me、失敗は例外) */
|
|
1026
|
+
async function appUser(ctx) {
|
|
1027
|
+
const res = await ctx.cms.fetch("/api/me");
|
|
1028
|
+
if (!res.ok) throw new Error(`app_user_unavailable: ${res.status}`);
|
|
1029
|
+
return await res.json();
|
|
824
1030
|
}
|
|
825
|
-
export { AUDIENCES, CUSTOMER_REF, CUSTOMER_STATUSES, IMPORT_ITEMS_MAX, JSON_BODY_MAX, ORIGIN_REF, appFailure, appIssue, appProblems, bodyFromHtml, cyclicCollections,
|
|
1031
|
+
export { AUDIENCES, CUSTOMERS_KEY_TARGET, CUSTOMER_REF, CUSTOMER_STATUSES, IMPORT_ITEMS_MAX, JSON_BODY_MAX, NO_SETTINGS, ORIGIN_REF, SettingProblem, allows, appAssertion, appFailure, appFailureOf, appInvalid, appIssue, appJson, appProblems, appUser, audiencesFor, bearerClient, bodyFromHtml, calendarFieldSource, canReadField, cyclicCollections, egressAllows, exchangeDelegation, importIdOf, importItemOfRow, isAdmin, isPublishable, newUuidV7, ownsRow, principalOfUser, titleFor, workspaceRecordPath };
|