@powerhousedao/codegen 4.1.0-dev.34 → 4.1.0-dev.36

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.
@@ -6,4 +6,12 @@ export * from './actions.js';
6
6
  export * from './document-model.js';
7
7
  export * from './object.js';
8
8
  export * from './types.js';
9
- export * as actions from './creators.js';
9
+ export * as actions from './creators.js';
10
+ export type { <%= h.changeCase.pascal(documentType) %>PHState } from './ph-factories.js';
11
+ export {
12
+ create<%= h.changeCase.pascal(documentType) %>Document,
13
+ createState,
14
+ defaultPHState,
15
+ defaultGlobalState,
16
+ defaultLocalState,
17
+ } from './ph-factories.js';
@@ -2,11 +2,11 @@
2
2
  to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/object.ts"
3
3
  force: true
4
4
  ---
5
- import { BaseDocumentClass, type BaseStateFromDocument, type PartialState, applyMixins, type SignalDispatch } from 'document-model';
6
- import { <%= 'type ' + h.changeCase.pascal(documentType) %>State, <%= 'type ' + h.changeCase.pascal(documentType) %>LocalState, <%= 'type ' + h.changeCase.pascal(documentType) %>Document } from './types.js';
5
+ import { BaseDocumentClass, applyMixins, type SignalDispatch } from 'document-model';
6
+ import { <%= h.changeCase.pascal(documentType) %>PHState } from './ph-factories.js';
7
7
  import { <%= 'type ' + h.changeCase.pascal(documentType) %>Action } from './actions.js';
8
8
  import { reducer } from './reducer.js';
9
- import utils from './utils.js';
9
+ import { createDocument } from './utils.js';
10
10
  <% modules.forEach(module => { _%>
11
11
  import <%= h.changeCase.pascal(documentType) %>_<%= h.changeCase.pascal(module.name) %> from './<%= module.name %>/object.js';
12
12
  <% }); _%>
@@ -20,11 +20,11 @@ interface <%= h.changeCase.pascal(documentType) %> extends
20
20
  <%= modules.map(m => ' ' + h.changeCase.pascal(documentType) + '_' + h.changeCase.pascal(m.name)).join(',\n') %> {}
21
21
 
22
22
  // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
23
- class <%= h.changeCase.pascal(documentType) %> extends BaseDocumentClass<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState, <%= h.changeCase.pascal(documentType) %>Action> {
23
+ class <%= h.changeCase.pascal(documentType) %> extends BaseDocumentClass<<%= h.changeCase.pascal(documentType) %>PHState> {
24
24
  static fileExtension = '<%= extension %>';
25
25
 
26
- constructor(initialState?: Partial<BaseStateFromDocument<<%= h.changeCase.pascal(documentType) %>Document>>, dispatch?: SignalDispatch) {
27
- super(reducer, utils.createDocument(initialState), dispatch);
26
+ constructor(initialState?: Partial<<%= h.changeCase.pascal(documentType) %>PHState>, dispatch?: SignalDispatch) {
27
+ super(reducer, createDocument(initialState), dispatch);
28
28
  }
29
29
 
30
30
  public saveToFile(path: string, name?: string) {
@@ -1,5 +1,5 @@
1
1
  ---
2
- to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/ph-factories.ts"
2
+ to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/ph-factories.ts"
3
3
  force: true
4
4
  ---
5
5
  /**
@@ -17,8 +17,13 @@ import type {
17
17
  <%= h.changeCase.pascal(documentType) %>Document,
18
18
  <%= h.changeCase.pascal(documentType) %>LocalState,
19
19
  <%= h.changeCase.pascal(documentType) %>State,
20
- } from "./gen/types.js";
21
- import { createDocument } from "./gen/utils.js";
20
+ } from "./types.js";
21
+ import { createDocument } from "./utils.js";
22
+
23
+ export type <%= h.changeCase.pascal(documentType) %>PHState = PHBaseState & {
24
+ global: <%= h.changeCase.pascal(documentType) %>State;
25
+ local: <%= h.changeCase.pascal(documentType) %>LocalState;
26
+ };
22
27
 
23
28
  export function defaultGlobalState(): <%= h.changeCase.pascal(documentType) %>State {
24
29
  return <%- initialGlobalState %>;
@@ -66,11 +71,6 @@ export function createState(
66
71
  };
67
72
  }
68
73
 
69
- export type <%= h.changeCase.pascal(documentType) %>PHState = PHBaseState & {
70
- global: <%= h.changeCase.pascal(documentType) %>State;
71
- local: <%= h.changeCase.pascal(documentType) %>LocalState;
72
- };
73
-
74
74
  /**
75
75
  * Creates a <%= h.changeCase.pascal(documentType) %>Document with custom global and local state
76
76
  * This properly handles the PHBaseState requirements while allowing
@@ -6,13 +6,14 @@ force: true
6
6
  /* eslint-disable @typescript-eslint/no-unsafe-member-access */
7
7
  /* eslint-disable @typescript-eslint/no-unsafe-argument */
8
8
  import { type StateReducer, isDocumentAction, createReducer } from "document-model";
9
- import { <%= 'type ' + h.changeCase.pascal(documentType) %>Document, z } from './types.js';
9
+ import { <%= h.changeCase.pascal(documentType) %>PHState } from './ph-factories.js';
10
+ import { z } from './types.js';
10
11
 
11
12
  <% modules.forEach(m => { _%>
12
13
  import { reducer as <%= h.changeCase.pascal(m.name) %>Reducer } from '../src/reducers/<%= h.changeCase.param(m.name) %>.js';
13
14
  <%_ }); %>
14
15
 
15
- const stateReducer: StateReducer<<%= h.changeCase.pascal(documentType) %>Document> =
16
+ export const stateReducer: StateReducer<<%= h.changeCase.pascal(documentType) %>PHState> =
16
17
  (state, action, dispatch) => {
17
18
  if (isDocumentAction(action)) {
18
19
  return state;
@@ -34,4 +35,4 @@ const stateReducer: StateReducer<<%= h.changeCase.pascal(documentType) %>Documen
34
35
  }
35
36
  }
36
37
 
37
- export const reducer = createReducer<<%= h.changeCase.pascal(documentType) %>Document>(stateReducer);
38
+ export const reducer = createReducer<<%= h.changeCase.pascal(documentType) %>PHState>(stateReducer);
@@ -13,15 +13,13 @@ import { reducer } from './gen/reducer.js';
13
13
  import { documentModel } from './gen/document-model.js';
14
14
  import genUtils from './gen/utils.js';
15
15
  import * as customUtils from './src/utils.js';
16
- import {
17
- <%= 'type ' + h.changeCase.pascal(documentType) %>Document,
18
- } from './gen/types.js';
16
+ import type { <%= h.changeCase.pascal(documentType) %>PHState } from './gen/ph-factories.js';
19
17
 
20
18
  const utils = { ...genUtils, ...customUtils };
21
19
  const actions = { ...BaseActions, ...<%= h.changeCase.pascal(documentType) %>Actions };
22
20
 
23
21
  export const module: DocumentModelModule<
24
- <%= h.changeCase.pascal(documentType) %>Document
22
+ <%= h.changeCase.pascal(documentType) %>PHState
25
23
  > = {
26
24
  reducer,
27
25
  actions,
@@ -2,18 +2,20 @@
2
2
  to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/types.ts"
3
3
  force: true
4
4
  ---
5
- import type { PHDocument, BaseStateFromDocument } from 'document-model';
6
- import type { <%= h.changeCase.pascal(documentType) %>State } from './schema/types.js';
5
+ import type { PHDocument } from 'document-model';
6
+ import type { <%= h.changeCase.pascal(documentType) %>Action } from './actions.js';
7
+ import type { <%= h.changeCase.pascal(documentType) %>PHState } from './ph-factories.js';
8
+ import type {
9
+ <%= h.changeCase.pascal(documentType) %>State,
7
10
  <% if(hasLocalSchema) { -%>
8
- import type { <%= h.changeCase.pascal(documentType) %>LocalState } from './schema/types.js';
11
+ <%= h.changeCase.pascal(documentType) %>LocalState,
9
12
  <%} -%>
10
- import type { <%= h.changeCase.pascal(documentType) %>Action } from './actions.js';
13
+ } from './schema/types.js';
11
14
 
12
15
  export { z } from './schema/index.js';
13
16
  export type * from './schema/types.js';
14
17
  <% if(!hasLocalSchema) { -%>
15
18
  <%= 'type ' + h.changeCase.pascal(documentType) %>LocalState = Record<PropertyKey, never>;
16
19
  <%} -%>
17
- export type Extended<%= h.changeCase.pascal(documentType) %>State = BaseStateFromDocument<<%= h.changeCase.pascal(documentType) %>Document>;
18
- export <%= 'type ' + h.changeCase.pascal(documentType) %>Document = PHDocument<<%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState>;
20
+ export type <%= h.changeCase.pascal(documentType) %>Document = PHDocument<<%= h.changeCase.pascal(documentType) %>PHState>;
19
21
  export type { <%= h.changeCase.pascal(documentType) %>State, <%= h.changeCase.pascal(documentType) %>LocalState, <%= h.changeCase.pascal(documentType) %>Action };
@@ -3,7 +3,10 @@ to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/utils.ts"
3
3
  force: true
4
4
  ---
5
5
  import {
6
- type DocumentModelUtils,
6
+ type CreateDocument,
7
+ type CreateState,
8
+ type LoadFromFile,
9
+ type LoadFromInput,
7
10
  baseCreateDocument,
8
11
  baseSaveToFile,
9
12
  baseSaveToFileHandle,
@@ -13,52 +16,55 @@ import {
13
16
  generateId,
14
17
  } from 'document-model';
15
18
  import {
16
- <%= 'type ' + h.changeCase.pascal(documentType) %>Document,
17
19
  <%= 'type ' + h.changeCase.pascal(documentType) %>State,
18
20
  <%= 'type ' + h.changeCase.pascal(documentType) %>LocalState
19
21
  } from './types.js';
22
+ import { <%= h.changeCase.pascal(documentType) %>PHState } from './ph-factories.js';
20
23
  import { reducer } from './reducer.js';
21
24
 
22
25
  export const initialGlobalState: <%= h.changeCase.pascal(documentType) %>State = <%- initialGlobalState %>;
23
26
  export const initialLocalState: <%= h.changeCase.pascal(documentType) %>LocalState = <%- initialLocalState %>;
24
27
 
25
- const utils: DocumentModelUtils<<%= h.changeCase.pascal(documentType) %>Document> = {
26
- fileExtension: '<%- fileExtension %>',
27
- createState(state) {
28
- return { ...defaultBaseState(), global: { ...initialGlobalState, ...state?.global }, local: { ...initialLocalState, ...state?.local } };
29
- },
30
- createDocument(state) {
31
- const document = baseCreateDocument(
32
- utils.createState,
33
- state
34
- );
28
+ export const createState: CreateState<<%= h.changeCase.pascal(documentType) %>PHState> = (state) => {
29
+ return {
30
+ ...defaultBaseState(),
31
+ global: { ...initialGlobalState, ...(state?.global ?? {}) },
32
+ local: { ...initialLocalState, ...(state?.local ?? {}) }
33
+ };
34
+ };
35
+
36
+ export const createDocument: CreateDocument<<%= h.changeCase.pascal(documentType) %>PHState> = (state) => {
37
+ const document = baseCreateDocument(createState, state);
38
+ document.header.documentType = '<%- documentTypeId %>';
39
+ // for backwards compatibility, but this is NOT a valid signed document id
40
+ document.header.id = generateId();
41
+ return document;
42
+ };
43
+
44
+ export const saveToFile = (document: any, path: string, name?: string) => {
45
+ return baseSaveToFile(document, path, '<%- fileExtension %>', name);
46
+ };
35
47
 
36
- document.header.documentType = '<%- documentTypeId %>';
48
+ export const saveToFileHandle = (document: any, input: any) => {
49
+ return baseSaveToFileHandle(document, input);
50
+ };
37
51
 
38
- // for backwards compatibility, but this is NOT a valid signed document id
39
- document.header.id = generateId();
52
+ export const loadFromFile: LoadFromFile<<%= h.changeCase.pascal(documentType) %>PHState> = (path) => {
53
+ return baseLoadFromFile(path, reducer);
54
+ };
40
55
 
41
- return document;
42
- },
43
- saveToFile(document, path, name) {
44
- return baseSaveToFile(document, path, '<%- fileExtension %>', name);
45
- },
46
- saveToFileHandle(document, input) {
47
- return baseSaveToFileHandle(document, input);
48
- },
49
- loadFromFile(path) {
50
- return baseLoadFromFile(path, reducer);
51
- },
52
- loadFromInput(input) {
53
- return baseLoadFromInput(input, reducer);
54
- },
56
+ export const loadFromInput: LoadFromInput<<%= h.changeCase.pascal(documentType) %>PHState> = (input) => {
57
+ return baseLoadFromInput(input, reducer);
55
58
  };
56
59
 
57
- export const createDocument = utils.createDocument;
58
- export const createState = utils.createState;
59
- export const saveToFile = utils.saveToFile;
60
- export const saveToFileHandle = utils.saveToFileHandle;
61
- export const loadFromFile = utils.loadFromFile;
62
- export const loadFromInput = utils.loadFromInput;
60
+ const utils = {
61
+ fileExtension: '<%- fileExtension %>',
62
+ createState,
63
+ createDocument,
64
+ saveToFile,
65
+ saveToFileHandle,
66
+ loadFromFile,
67
+ loadFromInput,
68
+ };
63
69
 
64
70
  export default utils;
@@ -3,12 +3,11 @@ to: "<%= rootDir %>/<%= h.changeCase.param(documentType) %>/gen/<%= module %>/ob
3
3
  force: true
4
4
  ---
5
5
  import { BaseDocumentClass<% if (actions.find(a => a.hasAttachment)) {%>, AttachmentInput<%}%> } from 'document-model';
6
+ import { <%= h.changeCase.pascal(documentType) %>PHState } from '../ph-factories.js';
6
7
  import {
7
8
  <% actions.filter(action => action.hasInput).forEach(action => { _%>
8
9
  <%= 'type ' + h.changeCase.pascal(action.name) %>Input,
9
10
  <% }); _%>
10
- <%= 'type ' + h.changeCase.pascal(documentType) %>State,
11
- <%= 'type ' + h.changeCase.pascal(documentType) %>LocalState
12
11
  } from '../types.js';
13
12
  import {
14
13
  <% actions.forEach(action => { _%>
@@ -17,11 +16,7 @@ import {
17
16
  } from './creators.js';
18
17
  import { <%= 'type ' + h.changeCase.pascal(documentType) %>Action } from '../actions.js';
19
18
 
20
- export default class <%= h.changeCase.pascal(documentType) %>_<%= h.changeCase.pascal(module) %> extends BaseDocumentClass<
21
- <%= h.changeCase.pascal(documentType) %>State,
22
- <%= h.changeCase.pascal(documentType) %>LocalState,
23
- <%= h.changeCase.pascal(documentType) %>Action
24
- > {
19
+ export default class <%= h.changeCase.pascal(documentType) %>_<%= h.changeCase.pascal(module) %> extends BaseDocumentClass<<%= h.changeCase.pascal(documentType) %>PHState> {
25
20
  <% actions.filter(action => action.hasInput).forEach(action => { _%>
26
21
  public <%= h.changeCase.camel(action.name) %>(input: <%= h.changeCase.pascal(action.name) %>Input<%if(action.hasAttachment){ %>, attachments: AttachmentInput[] <% } %>) {
27
22
  return this.dispatch(<%= h.changeCase.camel(action.name) %>(input<%if(action.hasAttachment){ %>, attachments<% } %>));
@@ -2,14 +2,9 @@
2
2
  to: "<%= rootDir %>/<%= h.changeCase.param(name) %>/index.ts"
3
3
  force: true
4
4
  ---
5
- import type { PHDocument } from "document-model";
6
5
  import { AnalyticsPath, AnalyticsSeriesInput, IAnalyticsStore } from "@powerhousedao/reactor-api";
7
6
  import { InternalTransmitterUpdate, IProcessor } from "document-drive";
8
7
 
9
- <% documentTypes.forEach(type => { _%>
10
- import type { <%= documentTypesMap[type].name %>Document } from "<%= documentTypesMap[type].importPath %>/index.js";
11
- %><% }); _%>
12
-
13
8
  export class <%= pascalName %>Processor implements IProcessor {
14
9
  private readonly NAMESPACE = "<%= pascalName %>";
15
10
 
@@ -19,7 +14,7 @@ export class <%= pascalName %>Processor implements IProcessor {
19
14
  //
20
15
  }
21
16
 
22
- async onStrands<TDocument extends PHDocument>(strands: InternalTransmitterUpdate<TDocument>[]): Promise<void> {
17
+ async onStrands(strands: InternalTransmitterUpdate[]): Promise<void> {
23
18
  if (strands.length === 0) {
24
19
  return;
25
20
  }
@@ -5,15 +5,9 @@ force: true
5
5
  import { type IRelationalDb } from "document-drive/processors/types";
6
6
  import { RelationalDbProcessor } from "document-drive/processors/relational";
7
7
  import { type InternalTransmitterUpdate } from "document-drive/server/listener/transmitter/internal";
8
- <% documentTypes.forEach(type => { _%>
9
- import type { <%= documentTypesMap[type].name %>Document } from "<%= documentTypesMap[type].importPath %>/index.js";
10
- %><% }); _%>
11
- <% if(documentTypes.length === 0) { %>import { type PHDocument } from "document-model";<% } %>
12
8
  import { up } from "./migrations.js";
13
9
  import { type DB } from "./schema.js";
14
10
 
15
- type DocumentType = <% if(documentTypes.length) { %><%= documentTypes.map(type => `${documentTypesMap[type].name}Document`).join(" | ") %> <% } else { %>PHDocument<% } %>;
16
-
17
11
  export class <%= pascalName %>Processor extends RelationalDbProcessor<DB> {
18
12
  static override getNamespace(driveId: string): string {
19
13
  // Default namespace: `${this.name}_${driveId.replaceAll("-", "_")}`
@@ -25,7 +19,7 @@ export class <%= pascalName %>Processor extends RelationalDbProcessor<DB> {
25
19
  }
26
20
 
27
21
  override async onStrands(
28
- strands: InternalTransmitterUpdate<DocumentType>[],
22
+ strands: InternalTransmitterUpdate[],
29
23
  ): Promise<void> {
30
24
  if (strands.length === 0) {
31
25
  return;