diff --git a/api/hooks/contact_form/post_create.js b/api/hooks/contact_form/post_create.js index dcc66ff..b279a19 100644 --- a/api/hooks/contact_form/post_create.js +++ b/api/hooks/contact_form/post_create.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var utils = require("../lib/utils") @@ -18,7 +15,7 @@ var utils = require("../lib/utils") } } - /** @type {import('../types').HookResponse} */ + /** @type {import('wmbasic-api-types').HookResponse} */ // @ts-ignore var response = null return response diff --git a/api/hooks/contact_form/post_return.js b/api/hooks/contact_form/post_return.js index 73f0b5f..14c01ce 100644 --- a/api/hooks/contact_form/post_return.js +++ b/api/hooks/contact_form/post_return.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var config = require("../config") var utils = require("../lib/utils") @@ -30,7 +27,7 @@ var utils = require("../lib/utils") }) } - /** @type {import('../types').HookResponse} */ + /** @type {import('wmbasic-api-types').HookResponse} */ // @ts-ignore var response = null return response diff --git a/api/hooks/content/delete_return.js b/api/hooks/content/delete_return.js index e671c5d..a686285 100644 --- a/api/hooks/content/delete_return.js +++ b/api/hooks/content/delete_return.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var utils = require("../lib/utils") diff --git a/api/hooks/content/post_return.js b/api/hooks/content/post_return.js index e671c5d..a686285 100644 --- a/api/hooks/content/post_return.js +++ b/api/hooks/content/post_return.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var utils = require("../lib/utils") diff --git a/api/hooks/content/put_return.js b/api/hooks/content/put_return.js index e671c5d..a686285 100644 --- a/api/hooks/content/put_return.js +++ b/api/hooks/content/put_return.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var utils = require("../lib/utils") diff --git a/api/hooks/lib/utils.js b/api/hooks/lib/utils.js index 6e4fcdd..d60fae1 100644 --- a/api/hooks/lib/utils.js +++ b/api/hooks/lib/utils.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var config = require("../config") @@ -25,7 +22,7 @@ function randomToken() { /** * - * @param {import('../types').HookContext} c + * @param {import('wmbasic-api-types').HookContext} c * @returns {boolean} */ function isPublicToken(c) { @@ -37,7 +34,7 @@ function isPublicToken(c) { /** * - * @param {import('../types').HookContext} c + * @param {import('wmbasic-api-types').HookContext} c * @returns {boolean} */ function isSsrToken(c) { @@ -49,7 +46,7 @@ function isSsrToken(c) { /** * - * @param {import('../types').HookContext} c + * @param {import('wmbasic-api-types').HookContext} c * @param {string} filename * @returns {string} */ diff --git a/api/hooks/ssr/get_read.js b/api/hooks/ssr/get_read.js index a95230d..cdf08ed 100644 --- a/api/hooks/ssr/get_read.js +++ b/api/hooks/ssr/get_read.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ var utils = require("../lib/utils") diff --git a/api/hooks/ssr/post_create.js b/api/hooks/ssr/post_create.js index eec09de..2c07118 100644 --- a/api/hooks/ssr/post_create.js +++ b/api/hooks/ssr/post_create.js @@ -1,7 +1,4 @@ // @ts-check -/** - * @typedef {import('../types') } - */ ;(function () { throw { diff --git a/api/hooks/types.d.ts b/api/hooks/types.d.ts deleted file mode 100644 index 45ecca3..0000000 --- a/api/hooks/types.d.ts +++ /dev/null @@ -1,247 +0,0 @@ -export interface CollectionDocument { - id?: string - insertTime?: Date - updateTime?: Date - [key: string]: any -} - -export interface ReadCollectionOptions { - filter?: { - [key: string]: any - } - selector?: { - [key: string]: any - } - projection?: string - offset?: number - limit?: number - sort?: string[] -} - -interface GetHookData { - /** - * true if only one document was requested via /COLLECTION/ID - */ - one?: boolean - /** - * get list of documents (only valid after stage "read" in "get" hook) - */ - results(): CollectionDocument[] - /** - * filter map only valid for "get" hooks - */ - filter?: { - [key: string]: any - } - /** - * selector map only valid for "get" hooks - */ - selector?: { - [key: string]: any - } - /** - * offset only valid for "get" hooks - */ - offset?: number - /** - * limit only valid for "get" hooks - */ - limit?: number - /** - * sort only valid for "get" hooks - */ - sort?: string[] | string -} - -interface PostHookData { - /** - * post data only valid in "post" and "put" hooks - */ - data?: CollectionDocument -} - -export interface HookContext extends GetHookData, PostHookData { - request(): { - method: string - remoteAddr: string - host: string - url: string - path: string - param(p: string): string - query(q: string): string - header(h: string): string - body(): string - } - - /** - * read results from a collection - * - * @param colName collection name - * @param options options map - */ - readCollection( - colName: string, - options?: ReadCollectionOptions - ): CollectionDocument[] - - /** - * read count of documents for filter from a collection - * - * @param colName collection name - * @param options options map (only filter is valid) - */ - readCollectionCount( - colName: string, - options?: ReadCollectionOptions - ): number - - /** - * create a document in a collection - * - * @param colName collection name - * @param data data map - */ - createDocument( - colName: string, - data: CollectionDocument - ): CollectionDocument - - /** - * update a document in a collection - * - * @param colName collection name - * @param id id of entry - * @param data new/changed data - */ - updateDocument( - colName: string, - id: string, - data: CollectionDocument - ): CollectionDocument - - /** - * deletes one document by id from collection - * - * @param colName collection name - * @param id id of entry - */ - deleteDocument(colName: string, id: string): { message: "ok" } - - /** - * deletes documents by filter from collection - * - * @param colName collection name - * @param options options map, only filter valid - */ - deleteDocuments( - colName: string, - options?: ReadCollectionOptions - ): { message: "ok"; removed: number } - - /** - * send an email - * - * @param options email options map - */ - mail(options: { - to: string | string[] - cc?: string | string[] - bcc?: string | string[] - subject?: string - from: string - fromName?: string - plain?: string - html?: string - attach?: string | string[] - }): void - - /** - * execute a template code and return result - * - * @param code template code - * @param contextData template context map - */ - template( - code: string, - contextData?: { - [key: string]: any - } - ): string - - /** - * read a file relative to config dir and return its content - * - * @param path relative file path - */ - file(path: string): string - - /** - * http request - * - * @param url url for request - * @param options request options - */ - fetch( - url: string, - options?: { - method?: string - headers?: { [key: string]: string } - body?: string - } - ): { - status: number - statusText: string - headers: { [key: string]: string } - trailer: { [key: string]: string } - url: string - body: { - text(): string - json(): any - } - } - - /** - * dumps data to header and server log - * - * @param toDump data to dump - */ - dump(...toDump: any): void - - /** - * set response header - * - * @param name header name - * @param value value - */ - header(name: string, value: any): void - - /** - * get JWT authentication - */ - auth(): { - id: string - username: string - role: number - permissions: string[] - } - - /** - * get Sentry trace id - */ - sentryTraceId(): string -} - -export interface HookException { - status?: number - html?: string - [key: string]: any -} - -export interface HookResponse extends GetHookData, PostHookData { - data?: CollectionDocument - results?: any -} - -declare global { - var context: HookContext -} diff --git a/package.json b/package.json index e72af73..5933622 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,8 @@ "svelte-routing": "^1.4.2", "svelte-scrollto": "^0.2.0", "tslib": "^2.0.3", - "typescript": "^4.1.2" + "typescript": "^4.1.2", + "wmbasic-api-types": "https://gitbase.de/cms/wmbasic-api-types.git" }, "dependencies": { "@sentry/browser": "^6.2.1", diff --git a/tsconfig.json b/tsconfig.json index b974d5a..71061af 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,7 +4,7 @@ "include": ["src/**/*", "types/**/*"], "compilerOptions": { "module": "esnext", - "typeRoots": ["./node_modules/@types", "./types"], + "typeRoots": ["./node_modules/@types", "./types", "wmbasic-api-types"], "target": "esnext", "moduleResolution": "node", diff --git a/yarn.lock b/yarn.lock index 8549dcf..dbaf031 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3613,6 +3613,10 @@ whatwg-url@^7.0.0: tr46 "^1.0.1" webidl-conversions "^4.0.2" +"wmbasic-api-types@https://gitbase.de/cms/wmbasic-api-types.git": + version "0.0.1" + resolved "https://gitbase.de/cms/wmbasic-api-types.git#8f988cf2acaee3a0d1c84109adce199234db3845" + word-wrap@~1.2.3: version "1.2.3" resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"