tw5-typed 0.2.0 → 0.2.1

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "description": "Types for tiddlywiki",
3
3
  "license": "MIT",
4
4
  "name": "tw5-typed",
5
- "version": "0.2.0",
5
+ "version": "0.2.1",
6
6
  "url": "https://github.com/tiddly-gittly/tw5-typed",
7
7
  "homepage": "https://github.com/tiddly-gittly/tw5-typed",
8
8
  "bugs": {
package/src/Tiddler.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  declare module 'tiddlywiki' {
2
2
  export class Tiddler {
3
- constructor(...fields: Array<Record<string, unknown> | Tiddler>);
3
+ /**
4
+ *
5
+ * @param tiddlers multiple tiddler fields or instances, will merge them to create a new one
6
+ */
7
+ constructor(...tiddlers: Array<Record<string, unknown> | Tiddler>);
4
8
  readonly cache: ITiddlerCache;
5
9
  readonly fields: ITiddlerFields;
6
10
  static fieldModules: Record<string, IModuleInfo>;
@@ -10,9 +14,9 @@ declare module 'tiddlywiki' {
10
14
 
11
15
  export interface ITiddlerFields {
12
16
  readonly [anyKey: string]: unknown;
13
- readonly color: string;
17
+ readonly color?: string;
14
18
  readonly created: Date;
15
- readonly list: string[];
19
+ readonly list?: string[];
16
20
  readonly modified: Date;
17
21
  readonly tags: string[];
18
22
  readonly text: string;
package/src/Wiki.d.ts CHANGED
@@ -10,7 +10,7 @@ declare module 'tiddlywiki' {
10
10
  */
11
11
  constructor(options: { enableIndexers: unknown[] });
12
12
  addIndexer(indexer: unknown, name: string): void;
13
- getTiddler: (title: string) => Tiddler | undefined;
13
+ getTiddler: <T extends Tiddler>(title: string) => T | undefined;
14
14
  /**
15
15
  * Get full list of tiddler titles in the wiki
16
16
  */
@@ -22,17 +22,18 @@ declare module 'tiddlywiki' {
22
22
  compileFilter: (filterString: string) => (iterator?: SourceIterator) => string[];
23
23
  /**
24
24
  * Set JSON tiddler, Object in data field will be JSON.stringify and put into the text.
25
+ * This will make tiddler to be JSON data tiddler `"type":"application/json"`, so if you just want to modify existed tiddler's data, use `addTiddler` instead.
25
26
  */
26
27
  setTiddlerData: (title: string, data?: object, fields?: ITiddlerFields, options?: any) => void;
27
28
  /**
28
29
  * Create or update tiddler.
29
30
  * Update existed tiddler based on the title field.
30
31
  */
31
- addTiddler: (tiddler: Tiddler | ITiddlerFields) => void;
32
+ addTiddler: (tiddler: Tiddler | Partial<ITiddlerFields>) => void;
32
33
  /**
33
34
  * Call `addTiddler` for each iton of the list
34
35
  */
35
- addTiddlers: (tiddler: Array<Tiddler | ITiddlerFields>) => void;
36
+ addTiddlers: (tiddler: Array<Tiddler | Partial<ITiddlerFields>>) => void;
36
37
  /**
37
38
  * Get tiddler's text field, with an optional default text.
38
39
  * If have _is_skinny field, will just return null (this is a rare case, so not put in the return type for now).
package/src/server.d.ts CHANGED
@@ -13,14 +13,91 @@ declare module 'tiddlywiki' {
13
13
  username: string;
14
14
  }
15
15
 
16
+ export interface IRoute {
17
+ handler: ServerEndpointHandler;
18
+ method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'OPTIONS' | 'HEAD';
19
+ path: RegExp;
20
+ }
21
+
22
+ export interface IServerOptions {
23
+ routes: IRoute[];
24
+ variables?: { [key: string]: string };
25
+ wiki: Wiki;
26
+ }
27
+
28
+ /**
29
+ A simple HTTP server with regexp-based routes
30
+
31
+ options: variables - optional hashmap of variables to set (a misnomer - they are really constant parameters)
32
+ routes - optional array of routes to use
33
+ wiki - reference to wiki object
34
+ */
35
+ export class Server {
36
+ constructor(options: IServerOptions);
37
+ routes: IRoute[];
38
+ addRoute(route: IRoute): void;
39
+ /**
40
+ * ```json
41
+ * {
42
+ port: "8080",
43
+ host: "127.0.0.1",
44
+ "required-plugins": "$:/plugins/tiddlywiki/filesystem,$:/plugins/tiddlywiki/tiddlyweb",
45
+ "root-tiddler": "$:/core/save/all",
46
+ "root-render-type": "text/plain",
47
+ "root-serve-type": "text/html",
48
+ "tiddler-render-type": "text/html",
49
+ "tiddler-render-template": "$:/core/templates/server/static.tiddler.html",
50
+ "system-tiddler-render-type": "text/plain",
51
+ "system-tiddler-render-template": "$:/core/templates/wikified-tiddler",
52
+ "debug-level": "none",
53
+ "gzip": "no",
54
+ "use-browser-cache": "no"
55
+ }
56
+ ```
57
+ */
58
+ defaultVariables: {
59
+ 'debug-level': string;
60
+ gzip: string;
61
+ host: string;
62
+ port: string;
63
+ 'required-plugins': string;
64
+ 'root-render-type': string;
65
+ 'root-serve-type': string;
66
+ 'root-tiddler': string;
67
+ 'system-tiddler-render-template': string;
68
+ 'system-tiddler-render-type': string;
69
+ 'tiddler-render-template': string;
70
+ 'tiddler-render-type': string;
71
+ 'use-browser-cache': string;
72
+ };
73
+
74
+ variables: Record<string, any>;
75
+ get(variableName: string): any;
76
+ requestHandler: ServerEndpointHandler;
77
+ /*
78
+ Listen for requests
79
+ port: optional port number (falls back to value of "port" variable)
80
+ host: optional host address (falls back to value of "host" variable)
81
+ prefix: optional prefix (falls back to value of "path-prefix" variable)
82
+ */
83
+ listen(port?: string, host?: string, prefix?: string): void;
84
+ /*
85
+ Check whether a given user is authorized for the specified authorizationType ("readers" or "writers"). Pass null or undefined as the username to check for anonymous access
86
+ */
87
+ isAuthorized(authorizationType: 'readers' | 'writers', username?: string | undefined): boolean;
88
+ close(): void;
89
+ }
90
+
16
91
  export interface ServerEndpointContext {
92
+ authenticatedUsername: string | undefined;
17
93
  data: string;
94
+ server: Server;
18
95
  wiki: Wiki;
19
96
  }
20
97
  /**
21
98
  * @link https://talk.tiddlywiki.org/t/what-is-the-state-in-server-route-handler/2877
22
99
  */
23
- export type ServerEndpointHandler<T = Record<string, any>> = (
100
+ export type ServerEndpointHandler<T = Record<string, unknown>> = (
24
101
  request: Http.ClientRequest,
25
102
  response: Http.ServerResponse,
26
103
  context: ServerEndpointContext & T,
package/src/utils.d.ts CHANGED
@@ -26,6 +26,8 @@ declare module 'tiddlywiki' {
26
26
  decodeURIComponentSafe(uri: string): string;
27
27
  /** Convert a URI encoded string to a string safely */
28
28
  decodeURISafe(uri: string): string;
29
+ /** the function behind `<<now "format">> */
30
+ formatDateString(date: Date, format: string): string;
29
31
  /** Fill in any null or undefined properties of an object with the properties from a list of source objects. Each property that is an object is called recursively */
30
32
  deepDefaults(object: object, ...sourceObjectList: object[]): object;
31
33
  /**
package/src/widget.d.ts CHANGED
@@ -8,6 +8,29 @@ declare module 'tiddlywiki' {
8
8
  modified: boolean;
9
9
  }
10
10
 
11
+ export interface IWidgetEvent {
12
+ /** widget event can carry any other parameters
13
+ *
14
+ * For example, `<$action-sendmessage $message="tw-mobile-sync-set-active-server-and-sync" title={{!!title}} />` will produce `paramObject: { title: "xxxx" }`
15
+ */
16
+ paramObject?: {
17
+ [othersParamKeys: string]: unknown;
18
+ };
19
+ /**
20
+ * Get `$param`
21
+ */
22
+ param?: string | undefined;
23
+ /** the first parameter of addEventListener
24
+ *
25
+ * For example, the `'open-command-palette'` in `$tw.rootWidget.addEventListener('open-command-palette', (e: IWidgetEvent) => this.openPalette(e));`
26
+ */
27
+ type: string;
28
+ widget: Widget;
29
+ /** maybe a DOM click event, if trigger by button click */
30
+ event: Event;
31
+ navigateFromTitle?: string;
32
+ }
33
+
11
34
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
12
35
  class variablesConstructor {}
13
36
 
@@ -24,17 +47,17 @@ declare module 'tiddlywiki' {
24
47
  variables: unknown;
25
48
  domNodes: Node[];
26
49
  /**
27
- Add an event listener
28
- */
29
- addEventListener(type: string, handler: (arguments_: unknown[]) => void): void;
50
+ Add an event listener
51
+ */
52
+ addEventListener(type: string, handler: (event: IWidgetEvent) => void | Promise<void>): void;
30
53
  /**
31
- Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget
32
- */
33
- dispatchEvent(type: string): void;
54
+ Dispatch an event to a widget. If the widget doesn't handle the event then it is also dispatched to the parent widget
55
+ */
56
+ dispatchEvent(typeOrEvent: string | Omit<IWidgetEvent, 'widget'>): void;
34
57
  /**
35
- Add a list of event listeners from an array [{type:,handler:},...]
36
- */
37
- addEventListeners(listeners: Array<{ handler: (arguments_: unknown[]) => void; type: string }>): void;
58
+ Add a list of event listeners from an array [{type:,handler:},...]
59
+ */
60
+ addEventListeners(listeners: Array<{ handler: (event: IWidgetEvent) => void | Promise<void>; type: string }>): void;
38
61
 
39
62
  parentDomNode: Node;
40
63
  execute: () => void;