@ankhorage/projects 0.2.1 → 0.3.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 +1 -1
- package/dist/capabilities/index.d.ts +11 -1
- package/dist/capabilities/index.js +9 -1
- package/dist/features/projects/application/projects.d.ts +1 -0
- package/dist/features/projects/application/projects.js +4 -0
- package/dist/features/projects/composition/createProjectsApiHandlerResolver.d.ts +10 -0
- package/dist/features/projects/composition/createProjectsApiHandlerResolver.js +227 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +19 -3
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
# @ankhorage/projects
|
|
5
5
|
|
|
6
|
-
         
|
|
7
7
|
|
|
8
8
|
Standalone project identity and repository actions.
|
|
9
9
|
|
|
@@ -59,10 +59,20 @@ export declare const CAPABILITIES: readonly [{
|
|
|
59
59
|
};
|
|
60
60
|
readonly label: "Reload project manifest";
|
|
61
61
|
readonly description: "Read the persisted project manifest draft and immutable revision history.";
|
|
62
|
+
}, {
|
|
63
|
+
readonly id: "projects.revision";
|
|
64
|
+
readonly owner: "@ankhorage/projects";
|
|
65
|
+
readonly access: readonly ["read"];
|
|
66
|
+
readonly binding: {
|
|
67
|
+
readonly kind: "storage";
|
|
68
|
+
readonly bindableAs: readonly ["target"];
|
|
69
|
+
};
|
|
70
|
+
readonly label: "Read project manifest revision";
|
|
71
|
+
readonly description: "Read one immutable project manifest revision by identity.";
|
|
62
72
|
}, {
|
|
63
73
|
readonly id: "projects.materialize";
|
|
64
74
|
readonly owner: "@ankhorage/projects";
|
|
65
|
-
readonly access: readonly ["
|
|
75
|
+
readonly access: readonly ["invoke"];
|
|
66
76
|
readonly binding: {
|
|
67
77
|
readonly kind: "action";
|
|
68
78
|
readonly bindableAs: readonly ["target"];
|
|
@@ -48,10 +48,18 @@ export const CAPABILITIES = [
|
|
|
48
48
|
label: 'Reload project manifest',
|
|
49
49
|
description: 'Read the persisted project manifest draft and immutable revision history.',
|
|
50
50
|
},
|
|
51
|
+
{
|
|
52
|
+
id: 'projects.revision',
|
|
53
|
+
owner: '@ankhorage/projects',
|
|
54
|
+
access: ['read'],
|
|
55
|
+
binding: { kind: 'storage', bindableAs: ['target'] },
|
|
56
|
+
label: 'Read project manifest revision',
|
|
57
|
+
description: 'Read one immutable project manifest revision by identity.',
|
|
58
|
+
},
|
|
51
59
|
{
|
|
52
60
|
id: 'projects.materialize',
|
|
53
61
|
owner: '@ankhorage/projects',
|
|
54
|
-
access: ['
|
|
62
|
+
access: ['invoke'],
|
|
55
63
|
binding: { kind: 'action', bindableAs: ['target'] },
|
|
56
64
|
label: 'Materialize project revision',
|
|
57
65
|
description: 'Materialize one stored immutable project revision through an injected owner generator.',
|
|
@@ -8,6 +8,7 @@ export declare const projects: {
|
|
|
8
8
|
update: (repository: ProjectRepository, input: UpdateProjectInput, now?: Date) => Promise<Project | null>;
|
|
9
9
|
save: (repository: ProjectRepository, input: SaveProjectManifestInput, now?: Date) => Promise<Project | null>;
|
|
10
10
|
reload: (repository: ProjectRepository, input: ReloadProjectManifestInput) => Promise<ProjectManifest | null>;
|
|
11
|
+
revision: (repository: ProjectRepository, input: MaterializeProjectRevisionInput) => Promise<ProjectManifestRevision | null>;
|
|
11
12
|
materialize: (repository: ProjectRepository, materializer: ProjectMaterializer, input: MaterializeProjectRevisionInput) => Promise<ProjectManifestRevision | null>;
|
|
12
13
|
delete: (repository: ProjectRepository, id: string) => Promise<Project | null>;
|
|
13
14
|
};
|
|
@@ -17,6 +17,10 @@ export const projects = {
|
|
|
17
17
|
update: async (repository, input, now = new Date()) => repository.updateAsync(input.id, (project) => updateProject(project, input, now)),
|
|
18
18
|
save: async (repository, input, now = new Date()) => repository.updateAsync(input.id, (project) => saveProjectManifest(project, input, now)),
|
|
19
19
|
reload: async (repository, input) => (await repository.readAsync(input.id))?.manifest ?? null,
|
|
20
|
+
revision: async (repository, input) => {
|
|
21
|
+
const project = await repository.readAsync(input.id);
|
|
22
|
+
return (project?.manifest.revisions.find((candidate) => candidate.id === input.revisionId) ?? null);
|
|
23
|
+
},
|
|
20
24
|
materialize: async (repository, materializer, input) => {
|
|
21
25
|
const project = await repository.readAsync(input.id);
|
|
22
26
|
const revision = project?.manifest.revisions.find((candidate) => candidate.id === input.revisionId);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ApiHandlerResolver } from '@ankhorage/api';
|
|
2
|
+
import type { ProjectMaterializer, ProjectRepository } from '../ports/ProjectRepository.js';
|
|
3
|
+
type CreateProjectsApiHandlerResolverInput = Readonly<{
|
|
4
|
+
repository: ProjectRepository;
|
|
5
|
+
materializer?: ProjectMaterializer;
|
|
6
|
+
allowDelete?: boolean;
|
|
7
|
+
}>;
|
|
8
|
+
/*** Create the Projects-owned resolver for host-approved server API operations. */
|
|
9
|
+
export declare function createProjectsApiHandlerResolver(input: CreateProjectsApiHandlerResolverInput): ApiHandlerResolver;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { isAppManifest } from '@ankhorage/contracts';
|
|
2
|
+
import { isRecord } from '@ankhorage/utility/object';
|
|
3
|
+
import { CAPABILITIES } from '../../../capabilities/index.js';
|
|
4
|
+
import { projects } from '../application/projects.js';
|
|
5
|
+
import { deriveProjectIdentity } from '../domain/deriveProjectIdentity.js';
|
|
6
|
+
/*** Create the Projects-owned resolver for host-approved server API operations. */
|
|
7
|
+
export function createProjectsApiHandlerResolver(input) {
|
|
8
|
+
return (request) => resolveProjectsApiHandler(input, request);
|
|
9
|
+
}
|
|
10
|
+
/*** Resolve only canonical Projects operations declared by the host API definition. */
|
|
11
|
+
function resolveProjectsApiHandler(input, request) {
|
|
12
|
+
const capability = CAPABILITIES.find((candidate) => candidate.id === request.declaration.capability);
|
|
13
|
+
const operation = capability && createProjectsApiOperations(input)[capability.id];
|
|
14
|
+
if (!capability ||
|
|
15
|
+
!operation ||
|
|
16
|
+
request.binding.operationId !== capability.id ||
|
|
17
|
+
request.binding.operation.intent !== operation.intent ||
|
|
18
|
+
request.binding.method !== operation.method ||
|
|
19
|
+
(capability.id === 'projects.delete' && input.allowDelete !== true)) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
capability,
|
|
24
|
+
approval: {
|
|
25
|
+
operationId: request.binding.operationId,
|
|
26
|
+
intent: operation.intent,
|
|
27
|
+
method: operation.method,
|
|
28
|
+
capabilityId: capability.id,
|
|
29
|
+
owner: capability.owner,
|
|
30
|
+
scope: request.context,
|
|
31
|
+
},
|
|
32
|
+
handler: operation.handler,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/*** Bind each fixed Projects capability to its existing owner action. */
|
|
36
|
+
function createProjectsApiOperations(input) {
|
|
37
|
+
return {
|
|
38
|
+
'projects.list': {
|
|
39
|
+
intent: 'read',
|
|
40
|
+
method: 'GET',
|
|
41
|
+
handler: async (_request, context) => ({
|
|
42
|
+
body: (await projects.list(input.repository)).filter((project) => project.id === context.composition?.projectId),
|
|
43
|
+
}),
|
|
44
|
+
},
|
|
45
|
+
'projects.read': createReadOperation(input.repository),
|
|
46
|
+
'projects.create': createCreateOperation(input.repository),
|
|
47
|
+
'projects.update': createUpdateOperation(input.repository),
|
|
48
|
+
'projects.save': createSaveOperation(input.repository),
|
|
49
|
+
'projects.reload': createReloadOperation(input.repository),
|
|
50
|
+
'projects.revision': createRevisionOperation(input.repository),
|
|
51
|
+
'projects.materialize': createMaterializeOperation(input.repository, input.materializer),
|
|
52
|
+
'projects.delete': createDeleteOperation(input.repository),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/*** Read exactly the project selected by the API composition scope. */
|
|
56
|
+
function createReadOperation(repository) {
|
|
57
|
+
return {
|
|
58
|
+
intent: 'read',
|
|
59
|
+
method: 'GET',
|
|
60
|
+
handler: async (request, context) => readProjectResponse(repository, request, context.composition?.projectId),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/*** Create only the project identity selected by the API composition scope. */
|
|
64
|
+
function createCreateOperation(repository) {
|
|
65
|
+
return {
|
|
66
|
+
intent: 'create',
|
|
67
|
+
method: 'POST',
|
|
68
|
+
handler: async (request, context) => {
|
|
69
|
+
const name = readName(request.body);
|
|
70
|
+
const projectId = context.composition?.projectId;
|
|
71
|
+
try {
|
|
72
|
+
if (!name || !projectId || deriveProjectIdentity(name) !== projectId) {
|
|
73
|
+
return invalidResponse('Project creation does not match the composition scope.');
|
|
74
|
+
}
|
|
75
|
+
return { status: 201, body: await projects.create(repository, { name }) };
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return projectActionFailureResponse(error);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/*** Update exactly the project selected by the API composition scope. */
|
|
84
|
+
function createUpdateOperation(repository) {
|
|
85
|
+
return {
|
|
86
|
+
intent: 'update',
|
|
87
|
+
method: 'PATCH',
|
|
88
|
+
handler: async (request, context) => {
|
|
89
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
90
|
+
const name = readName(request.body);
|
|
91
|
+
if (!id || !name)
|
|
92
|
+
return invalidResponse('Project update requires a scoped projectId and name.');
|
|
93
|
+
try {
|
|
94
|
+
return missingResponse(await projects.update(repository, { id, name }));
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
return projectActionFailureResponse(error);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
/*** Save a validated manifest only for the project selected by the composition scope. */
|
|
103
|
+
function createSaveOperation(repository) {
|
|
104
|
+
return {
|
|
105
|
+
intent: 'update',
|
|
106
|
+
method: 'PUT',
|
|
107
|
+
handler: async (request, context) => {
|
|
108
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
109
|
+
const body = readManifestSaveBody(request.body);
|
|
110
|
+
if (!id || !body)
|
|
111
|
+
return invalidResponse('Manifest save requires a scoped projectId and valid body.');
|
|
112
|
+
try {
|
|
113
|
+
return missingResponse(await projects.save(repository, { id, ...body }));
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
return projectActionFailureResponse(error);
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/*** Reload the draft and revision history only for the scoped project. */
|
|
122
|
+
function createReloadOperation(repository) {
|
|
123
|
+
return {
|
|
124
|
+
intent: 'read',
|
|
125
|
+
method: 'GET',
|
|
126
|
+
handler: async (request, context) => {
|
|
127
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
128
|
+
return id
|
|
129
|
+
? missingResponse(await projects.reload(repository, { id }))
|
|
130
|
+
: invalidResponse('Invalid project scope.');
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/*** Read one immutable revision only from the scoped project. */
|
|
135
|
+
function createRevisionOperation(repository) {
|
|
136
|
+
return {
|
|
137
|
+
intent: 'read',
|
|
138
|
+
method: 'GET',
|
|
139
|
+
handler: async (request, context) => {
|
|
140
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
141
|
+
const { revisionId } = request.params;
|
|
142
|
+
if (!id || !revisionId)
|
|
143
|
+
return invalidResponse('Revision lookup requires scoped projectId and revisionId.');
|
|
144
|
+
return missingResponse(await projects.revision(repository, { id, revisionId }));
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
/*** Delegate materialization only when the host explicitly provides its owner port. */
|
|
149
|
+
function createMaterializeOperation(repository, materializer) {
|
|
150
|
+
return {
|
|
151
|
+
intent: 'action',
|
|
152
|
+
method: 'POST',
|
|
153
|
+
handler: async (request, context) => {
|
|
154
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
155
|
+
const { revisionId } = request.params;
|
|
156
|
+
if (!id || !revisionId)
|
|
157
|
+
return invalidResponse('Materialization requires scoped projectId and revisionId.');
|
|
158
|
+
if (!materializer)
|
|
159
|
+
return unavailableResponse();
|
|
160
|
+
return missingResponse(await projects.materialize(repository, materializer, { id, revisionId }));
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/*** Delete only the scoped project after the host explicitly enables destructive resolution. */
|
|
165
|
+
function createDeleteOperation(repository) {
|
|
166
|
+
return {
|
|
167
|
+
intent: 'delete',
|
|
168
|
+
method: 'DELETE',
|
|
169
|
+
handler: async (request, context) => {
|
|
170
|
+
const id = readScopedProjectId(request, context.composition?.projectId);
|
|
171
|
+
return id
|
|
172
|
+
? missingResponse(await projects.delete(repository, id))
|
|
173
|
+
: invalidResponse('Invalid project scope.');
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/*** Read one project using only the resource id equal to the approved project scope. */
|
|
178
|
+
async function readProjectResponse(repository, request, projectId) {
|
|
179
|
+
const id = readScopedProjectId(request, projectId);
|
|
180
|
+
return id
|
|
181
|
+
? missingResponse(await projects.read(repository, id))
|
|
182
|
+
: invalidResponse('Invalid project scope.');
|
|
183
|
+
}
|
|
184
|
+
/*** Return a typed missing-resource response without exposing storage details. */
|
|
185
|
+
function missingResponse(value) {
|
|
186
|
+
return value === null
|
|
187
|
+
? { status: 404, body: { error: { code: 'project_not_found' } } }
|
|
188
|
+
: { body: value };
|
|
189
|
+
}
|
|
190
|
+
/*** Return an invalid-request response before a repository action receives untrusted input. */
|
|
191
|
+
function invalidResponse(message) {
|
|
192
|
+
return { status: 400, body: { error: { code: 'invalid_request', message } } };
|
|
193
|
+
}
|
|
194
|
+
/*** Return an explicit blocked result when no host materializer has been configured. */
|
|
195
|
+
function unavailableResponse() {
|
|
196
|
+
return { status: 503, body: { error: { code: 'materialization_unavailable' } } };
|
|
197
|
+
}
|
|
198
|
+
/*** Translate only expected owner-domain failures while preserving unexpected storage failures. */
|
|
199
|
+
function projectActionFailureResponse(error) {
|
|
200
|
+
if (!(error instanceof Error))
|
|
201
|
+
throw error;
|
|
202
|
+
if (error.message.includes('already exists') ||
|
|
203
|
+
error.message.startsWith('Project manifest revision conflict:')) {
|
|
204
|
+
return { status: 409, body: { error: { code: 'project_conflict' } } };
|
|
205
|
+
}
|
|
206
|
+
if (error.message.startsWith('A project name must')) {
|
|
207
|
+
return invalidResponse(error.message);
|
|
208
|
+
}
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
/*** Accept only a resource parameter exactly matching the API composition project scope. */
|
|
212
|
+
function readScopedProjectId(request, projectId) {
|
|
213
|
+
const id = request.params.projectId;
|
|
214
|
+
return id && id === projectId ? id : undefined;
|
|
215
|
+
}
|
|
216
|
+
/*** Read a non-empty project name from one untrusted API request body. */
|
|
217
|
+
function readName(value) {
|
|
218
|
+
return isRecord(value) && typeof value.name === 'string' ? value.name : undefined;
|
|
219
|
+
}
|
|
220
|
+
/*** Validate the one manifest-save body shape accepted by the Projects API handler. */
|
|
221
|
+
function readManifestSaveBody(value) {
|
|
222
|
+
if (!isRecord(value) || !isAppManifest(value.manifest))
|
|
223
|
+
return undefined;
|
|
224
|
+
if (value.expectedRevisionId !== null && typeof value.expectedRevisionId !== 'string')
|
|
225
|
+
return undefined;
|
|
226
|
+
return { manifest: value.manifest, expectedRevisionId: value.expectedRevisionId };
|
|
227
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { createProjectRepositoryFileSystemAsync } from './features/projects/adapters/createProjectRepositoryFileSystemAsync.js';
|
|
2
2
|
export { projects } from './features/projects/application/projects.js';
|
|
3
|
+
export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
|
|
3
4
|
export type { ProjectMaterializer, ProjectRepository, } from './features/projects/ports/ProjectRepository.js';
|
|
4
5
|
export type { CreateProjectInput, MaterializeProjectRevisionInput, Project, ProjectManifest, ProjectManifestDraft, ProjectManifestRevision, ReloadProjectManifestInput, SaveProjectManifestInput, UpdateProjectInput, } from './types/projects.js';
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { createProjectRepositoryFileSystemAsync } from './features/projects/adapters/createProjectRepositoryFileSystemAsync.js';
|
|
2
2
|
export { projects } from './features/projects/application/projects.js';
|
|
3
|
+
export { createProjectsApiHandlerResolver } from './features/projects/composition/createProjectsApiHandlerResolver.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ankhorage/projects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Standalone project identity and repository actions.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ankhorage",
|
|
@@ -61,7 +61,8 @@
|
|
|
61
61
|
"typescript": "~6.0.3"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@ankhorage/
|
|
64
|
+
"@ankhorage/api": "^0.5.1",
|
|
65
|
+
"@ankhorage/contracts": "^26.11.1",
|
|
65
66
|
"@ankhorage/utility": "^1.10.0"
|
|
66
67
|
},
|
|
67
68
|
"ankh": {
|
|
@@ -157,11 +158,26 @@
|
|
|
157
158
|
"label": "Reload project manifest",
|
|
158
159
|
"description": "Read the persisted project manifest draft and immutable revision history."
|
|
159
160
|
},
|
|
161
|
+
{
|
|
162
|
+
"id": "projects.revision",
|
|
163
|
+
"owner": "@ankhorage/projects",
|
|
164
|
+
"access": [
|
|
165
|
+
"read"
|
|
166
|
+
],
|
|
167
|
+
"binding": {
|
|
168
|
+
"kind": "storage",
|
|
169
|
+
"bindableAs": [
|
|
170
|
+
"target"
|
|
171
|
+
]
|
|
172
|
+
},
|
|
173
|
+
"label": "Read project manifest revision",
|
|
174
|
+
"description": "Read one immutable project manifest revision by identity."
|
|
175
|
+
},
|
|
160
176
|
{
|
|
161
177
|
"id": "projects.materialize",
|
|
162
178
|
"owner": "@ankhorage/projects",
|
|
163
179
|
"access": [
|
|
164
|
-
"
|
|
180
|
+
"invoke"
|
|
165
181
|
],
|
|
166
182
|
"binding": {
|
|
167
183
|
"kind": "action",
|