@plugmatica/plugin-build 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/LICENSE-APACHE +202 -0
- package/README.md +221 -2
- package/dev-target/README.md +20 -0
- package/dev-target/compose.yaml +119 -0
- package/dist/chunk-EOLUQEKQ.js +587 -0
- package/dist/chunk-QHTGR4ZB.js +9378 -0
- package/dist/chunk-TQNG5OZ3.js +3135 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +2606 -0
- package/dist/index.d.ts +2144 -0
- package/dist/index.js +230 -0
- package/dist/vite-WRwB4IG8.d.ts +929 -0
- package/dist/vite.d.ts +3 -0
- package/dist/vite.js +20 -0
- package/package.json +94 -4
- package/templates/README.md +29 -0
- package/templates/backend-capabilities/LICENSE.txt +231 -0
- package/templates/backend-capabilities/backend/Cargo.toml +21 -0
- package/templates/backend-capabilities/backend/src/lib.rs +179 -0
- package/templates/backend-capabilities/backend/tests/basic.rs +174 -0
- package/templates/backend-capabilities/manifest.source.json +64 -0
- package/templates/backend-capabilities/package.json +22 -0
- package/templates/backend-composition/LICENSE.txt +231 -0
- package/templates/backend-composition/backend/Cargo.toml +21 -0
- package/templates/backend-composition/backend/src/lib.rs +96 -0
- package/templates/backend-composition/backend/tests/basic.rs +75 -0
- package/templates/backend-composition/manifest.source.json +26 -0
- package/templates/backend-composition/package.json +22 -0
- package/templates/backend-entities/LICENSE.txt +231 -0
- package/templates/backend-entities/backend/Cargo.toml +20 -0
- package/templates/backend-entities/backend/src/lib.rs +77 -0
- package/templates/backend-entities/backend/tests/basic.rs +26 -0
- package/templates/backend-entities/manifest.source.json +15 -0
- package/templates/backend-entities/package.json +22 -0
- package/templates/backend-minimal/LICENSE.txt +231 -0
- package/templates/backend-minimal/backend/Cargo.toml +21 -0
- package/templates/backend-minimal/backend/src/lib.rs +69 -0
- package/templates/backend-minimal/backend/tests/basic.rs +67 -0
- package/templates/backend-minimal/manifest.source.json +15 -0
- package/templates/backend-minimal/package.json +22 -0
- package/templates/backend-operations/LICENSE.txt +231 -0
- package/templates/backend-operations/backend/Cargo.toml +21 -0
- package/templates/backend-operations/backend/src/lib.rs +117 -0
- package/templates/backend-operations/backend/tests/basic.rs +169 -0
- package/templates/backend-operations/manifest.source.json +15 -0
- package/templates/backend-operations/package.json +22 -0
- package/templates/frontend-entity/LICENSE.txt +27 -0
- package/templates/frontend-entity/README.md +46 -0
- package/templates/frontend-entity/frontend/i18n/en-GB.json +5 -0
- package/templates/frontend-entity/frontend/package.json +32 -0
- package/templates/frontend-entity/frontend/src/CustomerListPage.tsx +98 -0
- package/templates/frontend-entity/frontend/src/model.ts +5 -0
- package/templates/frontend-entity/frontend/tests/entityLifecycle.test.tsx +79 -0
- package/templates/frontend-entity/frontend/tsconfig.json +4 -0
- package/templates/frontend-entity/frontend/vite.config.ts +3 -0
- package/templates/frontend-entity/frontend/vitest.config.ts +9 -0
- package/templates/frontend-entity/manifest.source.json +53 -0
- package/templates/frontend-entity/package.json +22 -0
- package/templates/frontend-entity/pnpm-workspace.yaml +2 -0
- package/templates/frontend-extension/LICENSE.txt +27 -0
- package/templates/frontend-extension/README.md +7 -0
- package/templates/frontend-extension/frontend/i18n/en-GB.json +4 -0
- package/templates/frontend-extension/frontend/package.json +25 -0
- package/templates/frontend-extension/frontend/src/CustomerNotes.tsx +24 -0
- package/templates/frontend-extension/frontend/tsconfig.json +4 -0
- package/templates/frontend-extension/frontend/vite.config.ts +3 -0
- package/templates/frontend-extension/manifest.source.json +16 -0
- package/templates/frontend-extension/package.json +22 -0
- package/templates/frontend-extension/pnpm-workspace.yaml +2 -0
- package/templates/frontend-minimal/LICENSE.txt +27 -0
- package/templates/frontend-minimal/README.md +5 -0
- package/templates/frontend-minimal/frontend/i18n/en-GB.json +4 -0
- package/templates/frontend-minimal/frontend/package.json +25 -0
- package/templates/frontend-minimal/frontend/src/Page.tsx +16 -0
- package/templates/frontend-minimal/frontend/src/entry.ts +1 -0
- package/templates/frontend-minimal/frontend/tsconfig.json +4 -0
- package/templates/frontend-minimal/frontend/vite.config.ts +3 -0
- package/templates/frontend-minimal/manifest.source.json +51 -0
- package/templates/frontend-minimal/package.json +22 -0
- package/templates/frontend-minimal/pnpm-workspace.yaml +2 -0
- package/templates/frontend-provider-event/LICENSE.txt +27 -0
- package/templates/frontend-provider-event/README.md +7 -0
- package/templates/frontend-provider-event/frontend/i18n/en-GB.json +3 -0
- package/templates/frontend-provider-event/frontend/package.json +25 -0
- package/templates/frontend-provider-event/frontend/src/CustomerBadgeProvider.ts +24 -0
- package/templates/frontend-provider-event/frontend/src/CustomerSavedHandler.ts +22 -0
- package/templates/frontend-provider-event/frontend/tsconfig.json +4 -0
- package/templates/frontend-provider-event/frontend/vite.config.ts +3 -0
- package/templates/frontend-provider-event/manifest.source.json +16 -0
- package/templates/frontend-provider-event/package.json +22 -0
- package/templates/frontend-provider-event/pnpm-workspace.yaml +2 -0
- package/templates/full-stack-minimal/LICENSE.txt +231 -0
- package/templates/full-stack-minimal/README.md +49 -0
- package/templates/full-stack-minimal/backend/Cargo.toml +18 -0
- package/templates/full-stack-minimal/backend/src/lib.rs +49 -0
- package/templates/full-stack-minimal/backend/tests/basic.rs +49 -0
- package/templates/full-stack-minimal/frontend/i18n/en-GB.json +6 -0
- package/templates/full-stack-minimal/frontend/package.json +25 -0
- package/templates/full-stack-minimal/frontend/src/Page.tsx +29 -0
- package/templates/full-stack-minimal/frontend/src/entry.ts +1 -0
- package/templates/full-stack-minimal/frontend/tsconfig.json +4 -0
- package/templates/full-stack-minimal/frontend/vite.config.ts +3 -0
- package/templates/full-stack-minimal/manifest.source.json +55 -0
- package/templates/full-stack-minimal/package.json +24 -0
- package/templates/full-stack-minimal/pnpm-workspace.yaml +2 -0
- package/templates/full-stack-workflow-node/LICENSE.txt +231 -0
- package/templates/full-stack-workflow-node/README.md +111 -0
- package/templates/full-stack-workflow-node/backend/Cargo.toml +12 -0
- package/templates/full-stack-workflow-node/backend/src/lib.rs +70 -0
- package/templates/full-stack-workflow-node/frontend/i18n/en-GB.json +10 -0
- package/templates/full-stack-workflow-node/frontend/package.json +29 -0
- package/templates/full-stack-workflow-node/frontend/src/editors/FormatLabelEditor.tsx +25 -0
- package/templates/full-stack-workflow-node/frontend/src/entry.ts +2 -0
- package/templates/full-stack-workflow-node/frontend/src/nodes/FormatLabel.tsx +18 -0
- package/templates/full-stack-workflow-node/frontend/src/presentations/FormatLabelPresentation.tsx +7 -0
- package/templates/full-stack-workflow-node/frontend/tsconfig.json +4 -0
- package/templates/full-stack-workflow-node/frontend/vite.config.ts +5 -0
- package/templates/full-stack-workflow-node/manifest.source.json +18 -0
- package/templates/full-stack-workflow-node/package.json +25 -0
- package/templates/full-stack-workflow-node/pnpm-workspace.yaml +2 -0
- package/templates/localization-fr-fr/LICENSE.txt +27 -0
- package/templates/localization-fr-fr/README.md +5 -0
- package/templates/localization-fr-fr/localization/plugmatica.customer.fr-FR.json +4 -0
- package/templates/localization-fr-fr/manifest.source.json +36 -0
- package/templates/localization-fr-fr/package.json +18 -0
- package/templates/starter-projection.json +448 -0
- package/tsconfig.plugin.json +14 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "plugmatica-example-operations"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
license = "Apache-2.0"
|
|
6
|
+
publish = false
|
|
7
|
+
|
|
8
|
+
[lib]
|
|
9
|
+
crate-type = ["cdylib", "rlib"]
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
plugmatica-sdk = { version = "0.1.0" }
|
|
13
|
+
serde = { version = "1", features = ["derive"] }
|
|
14
|
+
serde_json = "1"
|
|
15
|
+
|
|
16
|
+
[dev-dependencies]
|
|
17
|
+
plugmatica-testing = { version = "0.1.0" }
|
|
18
|
+
plugmatica-protocol = { version = "0.1.0" }
|
|
19
|
+
|
|
20
|
+
# Build and test this example independently of the repository workspace.
|
|
21
|
+
[workspace]
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
use plugmatica_sdk::prelude::*;
|
|
2
|
+
|
|
3
|
+
#[derive(Deserialize)]
|
|
4
|
+
struct RouteQuery { verbose: Option<bool> }
|
|
5
|
+
#[derive(Deserialize)]
|
|
6
|
+
struct UpdateInput { name: String }
|
|
7
|
+
|
|
8
|
+
fn update_widget(_ctx: PluginContext, request: Request) -> Result<Response> {
|
|
9
|
+
let widget_pid = request.param("widgetPid")?;
|
|
10
|
+
let query: RouteQuery = request.query()?;
|
|
11
|
+
let input: UpdateInput = request.json()?;
|
|
12
|
+
if input.name.trim().is_empty() {
|
|
13
|
+
return Err(Error::bad_request("widget_name_required", "name is required")
|
|
14
|
+
.details(json!({"field": "name"})));
|
|
15
|
+
}
|
|
16
|
+
Response::ok().json(json!({
|
|
17
|
+
"pid": widget_pid,
|
|
18
|
+
"name": input.name,
|
|
19
|
+
"verbose": query.verbose.unwrap_or(false),
|
|
20
|
+
"requestId": request.header("x-request-id"),
|
|
21
|
+
}))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
fn rebuild(_ctx: PluginContext, payload: Value) -> Result<Value> {
|
|
25
|
+
Ok(json!({"accepted": true, "input": payload}))
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
fn summary(_ctx: PluginContext, payload: Value) -> Result<Value> {
|
|
29
|
+
Ok(json!({"count": 1, "filter": payload}))
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn preview(_ctx: PluginContext, payload: Value) -> Result<Value> {
|
|
33
|
+
Ok(json!({"preview": payload}))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// This job has a domain DTO; it still executes through the standard job owner.
|
|
37
|
+
// A fixed input error preserves the contract for missing/non-string executionKey.
|
|
38
|
+
#[derive(Deserialize)]
|
|
39
|
+
#[serde(rename_all = "camelCase")]
|
|
40
|
+
struct RebuildIndexInput {
|
|
41
|
+
execution_key: String,
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[derive(Serialize)]
|
|
45
|
+
#[serde(rename_all = "camelCase")]
|
|
46
|
+
struct RebuildIndexOutput {
|
|
47
|
+
accepted: bool,
|
|
48
|
+
execution_key: String,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
fn rebuild_index(_ctx: PluginContext, input: RebuildIndexInput) -> Result<RebuildIndexOutput> {
|
|
52
|
+
Ok(RebuildIndexOutput {
|
|
53
|
+
accepted: true,
|
|
54
|
+
execution_key: input.execution_key,
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#[plugin]
|
|
59
|
+
pub fn register_plugin(registry: &mut PluginRegistry) {
|
|
60
|
+
registry
|
|
61
|
+
.route_with_access(
|
|
62
|
+
Method::Patch,
|
|
63
|
+
"/widgets/{widgetPid}",
|
|
64
|
+
AccessPolicy::public().requiring_scope("widgets.write"),
|
|
65
|
+
update_widget,
|
|
66
|
+
)
|
|
67
|
+
.command_with_access(
|
|
68
|
+
"rebuild",
|
|
69
|
+
AccessPolicy::protected("widgets.admin").requiring_scope("widgets.admin"),
|
|
70
|
+
rebuild,
|
|
71
|
+
)
|
|
72
|
+
.query("summary", summary)
|
|
73
|
+
.custom_operation_with_access(
|
|
74
|
+
"widget_tools",
|
|
75
|
+
"preview",
|
|
76
|
+
AccessPolicy::internal(),
|
|
77
|
+
preview,
|
|
78
|
+
)
|
|
79
|
+
.job_typed_with_access(
|
|
80
|
+
"search.rebuild",
|
|
81
|
+
AccessPolicy::protected("widgets.admin").requiring_scope("widgets.admin"),
|
|
82
|
+
TypedOperation::new(rebuild_index)
|
|
83
|
+
.input_error("execution_key_required")
|
|
84
|
+
.input_message("executionKey is required"),
|
|
85
|
+
)
|
|
86
|
+
.workflow_action(
|
|
87
|
+
WorkflowAction::job(
|
|
88
|
+
"plugmatica.example-operations:action:rebuild-search",
|
|
89
|
+
"Rebuild search index",
|
|
90
|
+
"search.rebuild",
|
|
91
|
+
json!({
|
|
92
|
+
"type":"object",
|
|
93
|
+
"properties":{
|
|
94
|
+
"executionKey":{"type":"string","title":"Execution key"}
|
|
95
|
+
},
|
|
96
|
+
"required":["executionKey"],
|
|
97
|
+
"additionalProperties":false
|
|
98
|
+
}),
|
|
99
|
+
json!({
|
|
100
|
+
"type":"object",
|
|
101
|
+
"properties":{
|
|
102
|
+
"accepted":{"type":"boolean"},
|
|
103
|
+
"executionKey":{"type":"string"}
|
|
104
|
+
},
|
|
105
|
+
"required":["accepted","executionKey"],
|
|
106
|
+
"additionalProperties":false
|
|
107
|
+
}),
|
|
108
|
+
)
|
|
109
|
+
.description("Durable, retry-safe reference action backed by a plugin job.")
|
|
110
|
+
.category("Operations")
|
|
111
|
+
.idempotent(WorkflowRetryPolicy {
|
|
112
|
+
max_attempts: 3,
|
|
113
|
+
initial_backoff_seconds: 1,
|
|
114
|
+
max_backoff_seconds: 30,
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
use plugmatica_example_operations::register_plugin;
|
|
2
|
+
use plugmatica_protocol::{GuestInvocation, GuestInvocationValue, PluginHttpRequest, WorkflowNodeExecutor, WorkflowNodeIdempotency};
|
|
3
|
+
use plugmatica_testing::{assert_descriptor_valid, registry_from, TestRuntime};
|
|
4
|
+
use serde_json::json;
|
|
5
|
+
use std::collections::BTreeMap;
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
#[test]
|
|
9
|
+
fn http_parsing_and_structured_error_are_stable() {
|
|
10
|
+
let runtime = TestRuntime::new();
|
|
11
|
+
let registry = registry_from(register_plugin);
|
|
12
|
+
assert_descriptor_valid(®istry);
|
|
13
|
+
|
|
14
|
+
let failed = runtime.invoke("plugmatica.example-operations", "1.0.0", ®istry, GuestInvocation::Http {
|
|
15
|
+
request: PluginHttpRequest {
|
|
16
|
+
method: "PATCH".into(),
|
|
17
|
+
path: "/widgets/W-1".into(),
|
|
18
|
+
query: Some("verbose=true".into()),
|
|
19
|
+
body: Some(json!({"name": ""})),
|
|
20
|
+
headers: BTreeMap::from([("x-request-id".into(), "REQ-1".into())]),
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
assert!(!failed.ok);
|
|
24
|
+
let error = failed.error.expect("structured plugin error");
|
|
25
|
+
assert_eq!(error.code, "widget_name_required");
|
|
26
|
+
assert_eq!(error.status, Some(400));
|
|
27
|
+
assert_eq!(error.details, Some(json!({"field": "name"})));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#[test]
|
|
31
|
+
fn command_query_and_custom_operations_are_registered() {
|
|
32
|
+
let runtime = TestRuntime::new();
|
|
33
|
+
let registry = registry_from(register_plugin);
|
|
34
|
+
|
|
35
|
+
for invocation in [
|
|
36
|
+
GuestInvocation::Command { key: "rebuild".into(), payload: json!({"force": true}) },
|
|
37
|
+
GuestInvocation::Query { key: "summary".into(), payload: json!({"active": true}) },
|
|
38
|
+
GuestInvocation::Custom { surface: "widget_tools".into(), operation: "preview".into(), payload: json!({"pid": "W-1"}) },
|
|
39
|
+
] {
|
|
40
|
+
let response = runtime.invoke("plugmatica.example-operations", "1.0.0", ®istry, invocation);
|
|
41
|
+
assert!(response.ok, "operation failed: {response:?}");
|
|
42
|
+
assert!(matches!(response.value, Some(GuestInvocationValue::Json(_))));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#[test]
|
|
47
|
+
fn job_backed_workflow_action_is_distributable_and_retry_safe() {
|
|
48
|
+
let runtime = TestRuntime::new();
|
|
49
|
+
let registry = registry_from(register_plugin);
|
|
50
|
+
assert_descriptor_valid(®istry);
|
|
51
|
+
|
|
52
|
+
let descriptor = registry.descriptor();
|
|
53
|
+
let action = descriptor
|
|
54
|
+
.workflow_nodes
|
|
55
|
+
.iter()
|
|
56
|
+
.find(|node| node.node_type == "plugmatica.example-operations:action:rebuild-search")
|
|
57
|
+
.expect("job-backed workflow action");
|
|
58
|
+
assert_eq!(action.required_scopes, vec!["widgets.admin"]);
|
|
59
|
+
assert_eq!(action.idempotency, WorkflowNodeIdempotency::Idempotent);
|
|
60
|
+
assert_eq!(action.retry_policy.max_attempts, 3);
|
|
61
|
+
assert!(matches!(
|
|
62
|
+
action.executor.as_ref(),
|
|
63
|
+
Some(WorkflowNodeExecutor::Job { job }) if job == "search.rebuild"
|
|
64
|
+
));
|
|
65
|
+
|
|
66
|
+
let response = runtime.invoke(
|
|
67
|
+
"plugmatica.example-operations",
|
|
68
|
+
"1.0.0",
|
|
69
|
+
®istry,
|
|
70
|
+
GuestInvocation::Job {
|
|
71
|
+
key: "search.rebuild".into(),
|
|
72
|
+
payload: json!({"executionKey": "WF-EXEC-1"}),
|
|
73
|
+
},
|
|
74
|
+
);
|
|
75
|
+
assert!(response.ok, "job failed: {response:?}");
|
|
76
|
+
assert!(matches!(
|
|
77
|
+
response.value,
|
|
78
|
+
Some(GuestInvocationValue::Json(value))
|
|
79
|
+
if value == json!({"accepted": true, "executionKey": "WF-EXEC-1"})
|
|
80
|
+
));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#[test]
|
|
84
|
+
fn typed_job_rejects_missing_and_invalid_execution_keys_with_original_error() {
|
|
85
|
+
let runtime = TestRuntime::new();
|
|
86
|
+
let registry = registry_from(register_plugin);
|
|
87
|
+
for payload in [json!({}), json!({"executionKey": 42}), json!(null)] {
|
|
88
|
+
let response = runtime.invoke(
|
|
89
|
+
"plugmatica.example-operations",
|
|
90
|
+
"1.0.0",
|
|
91
|
+
®istry,
|
|
92
|
+
GuestInvocation::Job {
|
|
93
|
+
key: "search.rebuild".into(),
|
|
94
|
+
payload,
|
|
95
|
+
},
|
|
96
|
+
);
|
|
97
|
+
assert!(!response.ok);
|
|
98
|
+
let error = response.error.expect("typed input error");
|
|
99
|
+
assert_eq!(error.code, "execution_key_required");
|
|
100
|
+
assert_eq!(error.message, "executionKey is required");
|
|
101
|
+
assert_eq!(error.status, Some(400));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
#[test]
|
|
106
|
+
fn direct_typed_http_route_preserves_status_and_fixed_input_error() {
|
|
107
|
+
use plugmatica_sdk::prelude::{AccessPolicy, HttpInput, Method, PluginContext, PluginRegistry, Result, StatusCode};
|
|
108
|
+
use serde::{Deserialize, Serialize};
|
|
109
|
+
|
|
110
|
+
#[derive(Deserialize)]
|
|
111
|
+
struct CounterInput {
|
|
112
|
+
count: u32,
|
|
113
|
+
}
|
|
114
|
+
#[derive(Serialize)]
|
|
115
|
+
struct CounterOutput {
|
|
116
|
+
doubled: u32,
|
|
117
|
+
}
|
|
118
|
+
fn double(_ctx: PluginContext, input: CounterInput) -> Result<CounterOutput> {
|
|
119
|
+
Ok(CounterOutput { doubled: input.count * 2 })
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// This is a direct HTTP invocation, not a command dispatch. It does not
|
|
123
|
+
// create a second callable command or add command hooks to the endpoint.
|
|
124
|
+
let mut registry = PluginRegistry::new();
|
|
125
|
+
registry.route_typed_with_access(
|
|
126
|
+
Method::Post,
|
|
127
|
+
"/counter",
|
|
128
|
+
AccessPolicy::public(),
|
|
129
|
+
HttpInput::Body,
|
|
130
|
+
StatusCode::CREATED,
|
|
131
|
+
double,
|
|
132
|
+
);
|
|
133
|
+
assert_descriptor_valid(®istry);
|
|
134
|
+
let runtime = TestRuntime::new();
|
|
135
|
+
let invoke = |body| runtime.invoke(
|
|
136
|
+
"plugmatica.example-operations",
|
|
137
|
+
"1.0.0",
|
|
138
|
+
®istry,
|
|
139
|
+
GuestInvocation::Http {
|
|
140
|
+
request: PluginHttpRequest {
|
|
141
|
+
method: "POST".into(),
|
|
142
|
+
path: "/counter".into(),
|
|
143
|
+
query: None,
|
|
144
|
+
body,
|
|
145
|
+
headers: Default::default(),
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
let valid = invoke(Some(json!({"count": 4})));
|
|
151
|
+
assert!(valid.ok, "typed route failed: {valid:?}");
|
|
152
|
+
let GuestInvocationValue::Http(created) = valid.value.expect("HTTP response") else {
|
|
153
|
+
panic!("typed route must return an HTTP value");
|
|
154
|
+
};
|
|
155
|
+
assert_eq!(created.status, 201);
|
|
156
|
+
assert_eq!(created.body, json!({"doubled": 8}));
|
|
157
|
+
assert!(registry.descriptor().commands.is_empty());
|
|
158
|
+
|
|
159
|
+
let malformed = invoke(Some(json!({"count": "nope"})));
|
|
160
|
+
assert!(!malformed.ok);
|
|
161
|
+
let error = malformed.error.expect("malformed typed HTTP input");
|
|
162
|
+
assert_eq!(error.code, "invalid_json_body");
|
|
163
|
+
assert_eq!(error.status, Some(400));
|
|
164
|
+
assert!(error.message.starts_with("Request JSON body is invalid:"));
|
|
165
|
+
|
|
166
|
+
let missing = invoke(None);
|
|
167
|
+
assert!(!missing.ok);
|
|
168
|
+
assert_eq!(missing.error.expect("missing body").code, "missing_json_body");
|
|
169
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"key": "plugmatica.example-operations",
|
|
3
|
+
"name": "Backend Operations Example",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Compile-tested example for routes, commands, queries, custom operations and access policies.",
|
|
6
|
+
"author": "Plugmatica",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"plugins": []
|
|
9
|
+
},
|
|
10
|
+
"permissions": [],
|
|
11
|
+
"backend": {
|
|
12
|
+
"entryPoint": "backend/plugin.wasm",
|
|
13
|
+
"capabilities": []
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "plugmatica-plugin",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"plugin:package": "plugmatica package",
|
|
8
|
+
"plugin:install": "plugmatica install",
|
|
9
|
+
"plugin:dev": "plugmatica dev install",
|
|
10
|
+
"dev:target": "plugmatica dev up",
|
|
11
|
+
"backend:test": "cargo test --manifest-path backend/Cargo.toml",
|
|
12
|
+
"backend:build": "plugmatica backend build",
|
|
13
|
+
"check": "cargo test --manifest-path backend/Cargo.toml",
|
|
14
|
+
"build": "plugmatica backend build"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@plugmatica/plugin-build": "^0.1.0"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=22"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Plugmatica starter source licensing
|
|
2
|
+
|
|
3
|
+
The Plugmatica-authored files supplied with this starter are MIT licensed.
|
|
4
|
+
Preserve the applicable copyright and licence notices for reused code.
|
|
5
|
+
These terms do not prescribe a licence for your own original code.
|
|
6
|
+
|
|
7
|
+
MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) Plugmatica contributors
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Frontend entity starter
|
|
2
|
+
|
|
3
|
+
A typed, cancellation-aware frontend example for the current plugin's `customer`
|
|
4
|
+
entity. It uses `ownerEntity()` from the public SDK, `useAsyncResource()` and
|
|
5
|
+
`useResourceTable()` from Blocks, and one compiler-visible `CollectionBlock`.
|
|
6
|
+
|
|
7
|
+
- `ownerEntity<Customer>(api, "customer").list({ limit, cursor }, { signal })`
|
|
8
|
+
preserves the runtime's owner-scoped entity route, cancellation and cursor
|
|
9
|
+
contract. `nextCursor` is not a total count: the page offers Previous/Next
|
|
10
|
+
controls instead of pretending a server page is the entire dataset.
|
|
11
|
+
- A changed host API owner clears the resource immediately; outstanding reads
|
|
12
|
+
cannot commit stale rows. A refresh keeps same-scope content, and a failed
|
|
13
|
+
read shows the standard safe feedback with Retry. The UI never prints raw
|
|
14
|
+
server errors.
|
|
15
|
+
- Columns and `getRowId` have stable domain keys. `customer.list.table` is an
|
|
16
|
+
intentional public identity retained from the original example. The new
|
|
17
|
+
Previous/Next controls have separate static action identities.
|
|
18
|
+
- The backend-owned entity schema and access policy are authoritative. This
|
|
19
|
+
**frontend-only** starter demonstrates the *consumer* of a plugin-owned
|
|
20
|
+
entity; to make the list function in an installed package, add that entity
|
|
21
|
+
to the same package's backend facet. It does not create a cross-plugin
|
|
22
|
+
package dependency or fabricate a sample entity from browser state.
|
|
23
|
+
|
|
24
|
+
For a resource response that needs query caching and cross-screen invalidation
|
|
25
|
+
instead, use `@plugmatica/plugin-query`, not another `useAsyncResource` cache.
|
|
26
|
+
For entity creation, use the same package-owned `ownerEntity().create(value)`
|
|
27
|
+
API with `useCrudEditor` and `FormBlock`; preserve write failure as a rejected
|
|
28
|
+
Form submission and only call `resource.refresh()` after confirmed persistence.
|
|
29
|
+
|
|
30
|
+
From an installed SDK checkout or scaffolded project, run the example frontend
|
|
31
|
+
test, typecheck and build commands. The SDK contributor workflow also checks
|
|
32
|
+
the portable projection; do not edit its generated templates independently.
|
|
33
|
+
|
|
34
|
+
## Executable example tests
|
|
35
|
+
|
|
36
|
+
`frontend/tests/entityLifecycle.test.tsx` runs with Vitest, React Testing Library
|
|
37
|
+
and the **public** `@plugmatica/plugin-testing` runtime harness. It exercises
|
|
38
|
+
owner-scoped requests, opaque cursor navigation, empty results and safe error
|
|
39
|
+
feedback against the rendered page, rather than inspecting implementation text.
|
|
40
|
+
The test is intentionally outside `src/`, so production TypeScript compilation
|
|
41
|
+
and the manifest/compiler scan do not treat test-only JSX as an exposed module.
|
|
42
|
+
|
|
43
|
+
After installing the example frontend's dependencies in an appropriate
|
|
44
|
+
development workspace, run `pnpm --dir frontend test`. The portable template
|
|
45
|
+
projects the same test and public semver dependencies for independently
|
|
46
|
+
scaffolded plugins. These tests do **not** require a running backend database.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plugmatica/example-frontend-entity",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "vite build",
|
|
8
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
9
|
+
"test": "vitest run"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@plugmatica/blocks": "^0.1.0",
|
|
13
|
+
"@plugmatica/i18n": "^0.1.0",
|
|
14
|
+
"@plugmatica/plugin-sdk": "^0.1.0",
|
|
15
|
+
"@plugmatica/ui-kit": "^0.1.0",
|
|
16
|
+
"react": "^19.1.1",
|
|
17
|
+
"react-dom": "^19.1.1",
|
|
18
|
+
"react-hook-form": "^7.71.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@plugmatica/plugin-build": "^0.1.0",
|
|
22
|
+
"@plugmatica/plugin-testing": "^0.1.0",
|
|
23
|
+
"@tanstack/react-query": "^5.90.21",
|
|
24
|
+
"@testing-library/react": "^16.3.2",
|
|
25
|
+
"@types/react": "^19.2.2",
|
|
26
|
+
"@types/react-dom": "^19.2.2",
|
|
27
|
+
"jsdom": "^28.1.0",
|
|
28
|
+
"typescript": "^5.9.3",
|
|
29
|
+
"vite": "^8.2.0",
|
|
30
|
+
"vitest": "^4.0.18"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ownerEntity,
|
|
3
|
+
PluginMessage,
|
|
4
|
+
usePluginApi,
|
|
5
|
+
usePluginI18n,
|
|
6
|
+
type EntityListResponse,
|
|
7
|
+
} from "@plugmatica/plugin-sdk";
|
|
8
|
+
import { CollectionBlock, useAsyncResource, useResourceTable } from "@plugmatica/blocks";
|
|
9
|
+
import { Button, Page, PageHeader, Stack } from "@plugmatica/ui-kit";
|
|
10
|
+
import * as React from "react";
|
|
11
|
+
|
|
12
|
+
import type { Customer } from "./model";
|
|
13
|
+
|
|
14
|
+
// Static descriptors retain stable, compiler-visible column keys and i18n IDs.
|
|
15
|
+
const columns = [
|
|
16
|
+
{
|
|
17
|
+
accessorKey: "name",
|
|
18
|
+
header: () => <PluginMessage id="$common.fields.name" />,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
accessorKey: "email",
|
|
22
|
+
header: () => <PluginMessage id="$common.fields.email" />,
|
|
23
|
+
},
|
|
24
|
+
] as const;
|
|
25
|
+
|
|
26
|
+
const PAGE_SIZE = 50;
|
|
27
|
+
const firstPage = () => ({ index: 0, cursors: [null] as (string | null)[] });
|
|
28
|
+
const selectCustomers = (page: EntityListResponse<Customer>) =>
|
|
29
|
+
page.items.map((item) => item.value);
|
|
30
|
+
|
|
31
|
+
export default function CustomerListPage() {
|
|
32
|
+
const api = usePluginApi();
|
|
33
|
+
const { t } = usePluginI18n();
|
|
34
|
+
// Cursor history is local UI state, never a fabricated total or a client-side
|
|
35
|
+
// slice of one server page. Scope it to the host-provided API owner.
|
|
36
|
+
const [paging, setPaging] = React.useState(() => ({ owner: api, ...firstPage() }));
|
|
37
|
+
const current = paging.owner === api ? paging : { owner: api, ...firstPage() };
|
|
38
|
+
const cursor = current.cursors[current.index] ?? null;
|
|
39
|
+
|
|
40
|
+
const load = React.useCallback(
|
|
41
|
+
(signal: AbortSignal) =>
|
|
42
|
+
ownerEntity<Customer>(api, "customer").list(
|
|
43
|
+
{ limit: PAGE_SIZE, cursor },
|
|
44
|
+
{ signal },
|
|
45
|
+
),
|
|
46
|
+
[api, cursor],
|
|
47
|
+
);
|
|
48
|
+
const resource = useAsyncResource(load, { scopeKey: api });
|
|
49
|
+
const table = useResourceTable(resource, selectCustomers);
|
|
50
|
+
|
|
51
|
+
const previous = () => {
|
|
52
|
+
if (resource.loading || resource.error || current.index === 0) return;
|
|
53
|
+
setPaging({ ...current, index: current.index - 1 });
|
|
54
|
+
};
|
|
55
|
+
const next = () => {
|
|
56
|
+
const nextCursor = resource.value?.nextCursor;
|
|
57
|
+
if (resource.loading || resource.error || !nextCursor) return;
|
|
58
|
+
setPaging({
|
|
59
|
+
owner: api,
|
|
60
|
+
index: current.index + 1,
|
|
61
|
+
cursors: [...current.cursors.slice(0, current.index + 1), nextCursor],
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Page>
|
|
67
|
+
<PageHeader title={t("customers.title")} description={t("customers.description")} />
|
|
68
|
+
<CollectionBlock<Customer>
|
|
69
|
+
{...table}
|
|
70
|
+
extension="customer.list.table"
|
|
71
|
+
columns={columns}
|
|
72
|
+
getRowId={(customer) => customer.pid}
|
|
73
|
+
hidePagination
|
|
74
|
+
empty={{ title: t("customers.empty") }}
|
|
75
|
+
/>
|
|
76
|
+
{(current.index > 0 || Boolean(resource.value?.nextCursor)) && (
|
|
77
|
+
<Stack direction="row" gap="sm">
|
|
78
|
+
<Button
|
|
79
|
+
extension="customer.list.previous"
|
|
80
|
+
variant="outlined"
|
|
81
|
+
disabled={resource.loading || Boolean(resource.error) || current.index === 0}
|
|
82
|
+
onClick={previous}
|
|
83
|
+
>
|
|
84
|
+
{t("$common.actions.previous")}
|
|
85
|
+
</Button>
|
|
86
|
+
<Button
|
|
87
|
+
extension="customer.list.next"
|
|
88
|
+
variant="outlined"
|
|
89
|
+
disabled={resource.loading || Boolean(resource.error) || !resource.value?.nextCursor}
|
|
90
|
+
onClick={next}
|
|
91
|
+
>
|
|
92
|
+
{t("$common.actions.next")}
|
|
93
|
+
</Button>
|
|
94
|
+
</Stack>
|
|
95
|
+
)}
|
|
96
|
+
</Page>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { afterEach, expect, test } from "vitest";
|
|
4
|
+
import { cleanup, fireEvent, screen, waitFor } from "@testing-library/react";
|
|
5
|
+
import { createMockScopedPluginApi, renderPlugin } from "@plugmatica/plugin-testing";
|
|
6
|
+
import CustomerListPage from "../src/CustomerListPage";
|
|
7
|
+
|
|
8
|
+
afterEach(cleanup);
|
|
9
|
+
|
|
10
|
+
const envelope = (pid: string, name: string) => ({
|
|
11
|
+
value: { pid, name, email: `${pid}@example.invalid` },
|
|
12
|
+
revision: 1,
|
|
13
|
+
schemaVersion: 1,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// The example is also the source of the published frontend-entity scaffold.
|
|
17
|
+
// Exercise the public owner-scoped client and real Blocks resource lifecycle,
|
|
18
|
+
// rather than checking what the component source happens to look like.
|
|
19
|
+
test("loads the current owner's entities with a bounded first-page request", async () => {
|
|
20
|
+
const api = createMockScopedPluginApi({
|
|
21
|
+
handlers: {
|
|
22
|
+
"GET /_entities/customer?limit=50": () => ({
|
|
23
|
+
items: [envelope("customer-a", "Acme Customer")],
|
|
24
|
+
nextCursor: null,
|
|
25
|
+
}),
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
renderPlugin(<CustomerListPage />, { api, pluginIdentifier: "acme.customer" });
|
|
29
|
+
expect(await screen.findByText("Acme Customer")).toBeTruthy();
|
|
30
|
+
expect(api.requests).toEqual([
|
|
31
|
+
{ method: "GET", path: "/_entities/customer?limit=50", body: undefined },
|
|
32
|
+
]);
|
|
33
|
+
expect(screen.queryByRole("button", { name: /next/i })).toBeNull();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("navigates using opaque server cursors, then returns to the previous page", async () => {
|
|
37
|
+
const api = createMockScopedPluginApi({
|
|
38
|
+
handlers: {
|
|
39
|
+
"GET /_entities/customer?limit=50": () => ({
|
|
40
|
+
items: [envelope("customer-a", "First Customer")],
|
|
41
|
+
nextCursor: "page/two",
|
|
42
|
+
}),
|
|
43
|
+
"GET /_entities/customer?limit=50&cursor=page%2Ftwo": () => ({
|
|
44
|
+
items: [envelope("customer-b", "Second Customer")],
|
|
45
|
+
nextCursor: null,
|
|
46
|
+
}),
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
renderPlugin(<CustomerListPage />, { api, pluginIdentifier: "acme.customer" });
|
|
50
|
+
expect(await screen.findByText("First Customer")).toBeTruthy();
|
|
51
|
+
fireEvent.click(screen.getByRole("button", { name: /next/i }));
|
|
52
|
+
expect(await screen.findByText("Second Customer")).toBeTruthy();
|
|
53
|
+
expect(screen.queryByText("First Customer")).toBeNull();
|
|
54
|
+
fireEvent.click(screen.getByRole("button", { name: /previous/i }));
|
|
55
|
+
expect(await screen.findByText("First Customer")).toBeTruthy();
|
|
56
|
+
expect(api.requests.map(({ path }) => path)).toEqual([
|
|
57
|
+
"/_entities/customer?limit=50",
|
|
58
|
+
"/_entities/customer?limit=50&cursor=page%2Ftwo",
|
|
59
|
+
"/_entities/customer?limit=50",
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("renders the shared empty feedback for an empty server page", async () => {
|
|
64
|
+
const api = createMockScopedPluginApi({
|
|
65
|
+
defaultHandler: () => ({ items: [], nextCursor: null }),
|
|
66
|
+
});
|
|
67
|
+
renderPlugin(<CustomerListPage />, { api, pluginIdentifier: "acme.customer" });
|
|
68
|
+
expect(await screen.findByText("customers.empty")).toBeTruthy();
|
|
69
|
+
expect(screen.queryByRole("button", { name: /next/i })).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("a rejected entity read never prints the transport's private error", async () => {
|
|
73
|
+
const api = createMockScopedPluginApi({
|
|
74
|
+
defaultHandler: () => { throw new Error("secret-token-do-not-render"); },
|
|
75
|
+
});
|
|
76
|
+
renderPlugin(<CustomerListPage />, { api, pluginIdentifier: "acme.customer" });
|
|
77
|
+
await waitFor(() => expect(screen.getByRole("alert")).toBeTruthy());
|
|
78
|
+
expect(screen.queryByText(/secret-token-do-not-render/)).toBeNull();
|
|
79
|
+
});
|