From cbb25088a372306fff5b121716c18c4ff38a30f9 Mon Sep 17 00:00:00 2001 From: Pavel Denisjuk Date: Thu, 28 May 2020 23:58:30 +0200 Subject: [PATCH] feat: introduce api-settings-manager package --- package.json | 3 +- packages/api-cookie-policy/package.json | 3 +- packages/api-cookie-policy/src/index.ts | 15 +++++- .../api-cookie-policy/tsconfig.build.json | 2 + packages/api-cookie-policy/tsconfig.json | 1 + packages/api-form-builder/package.json | 1 + .../src/plugins/graphql/formsSettings.ts | 15 +++++- packages/api-form-builder/tsconfig.build.json | 2 + packages/api-form-builder/tsconfig.json | 1 + packages/api-google-tag-manager/package.json | 1 + packages/api-google-tag-manager/src/index.ts | 15 +++++- .../tsconfig.build.json | 10 +++- packages/api-google-tag-manager/tsconfig.json | 1 + packages/api-mailchimp/package.json | 1 + packages/api-mailchimp/src/index.ts | 20 ++++++-- packages/api-mailchimp/tsconfig.build.json | 11 +++- packages/api-mailchimp/tsconfig.json | 1 + packages/api-page-builder/package.json | 1 + .../src/plugins/graphql/Settings.ts | 15 +++++- packages/api-page-builder/tsconfig.build.json | 2 + packages/api-page-builder/tsconfig.json | 1 + packages/api-settings-manager/.babelrc.js | 1 + packages/api-settings-manager/LICENSE | 21 ++++++++ packages/api-settings-manager/README.md | 1 + packages/api-settings-manager/package.json | 40 +++++++++++++++ .../src/client/SettingsManagerClient.ts | 50 +++++++++++++++++++ .../api-settings-manager/src/client/index.ts | 18 +++++++ .../api-settings-manager/src/handler/index.ts | 47 +++++++++++++++++ packages/api-settings-manager/src/types.ts | 7 +++ .../api-settings-manager/tsconfig.build.json | 13 +++++ packages/api-settings-manager/tsconfig.json | 4 ++ tsconfig.build.json | 1 + 32 files changed, 308 insertions(+), 17 deletions(-) create mode 100644 packages/api-settings-manager/.babelrc.js create mode 100644 packages/api-settings-manager/LICENSE create mode 100644 packages/api-settings-manager/README.md create mode 100644 packages/api-settings-manager/package.json create mode 100644 packages/api-settings-manager/src/client/SettingsManagerClient.ts create mode 100644 packages/api-settings-manager/src/client/index.ts create mode 100644 packages/api-settings-manager/src/handler/index.ts create mode 100644 packages/api-settings-manager/src/types.ts create mode 100644 packages/api-settings-manager/tsconfig.build.json create mode 100644 packages/api-settings-manager/tsconfig.json diff --git a/package.json b/package.json index dbe30018258..ef25f40a29e 100644 --- a/package.json +++ b/package.json @@ -5,11 +5,12 @@ "packages/*", "sample-project/apps/*", "sample-project/api/apolloGateway", + "sample-project/api/settingsManager", "sample-project/api/databaseProxy", "sample-project/api/files/*", "sample-project/api/formBuilder", "sample-project/api/cms/*", - "sample-project/api/i18n", + "sample-project/api/i18n/*", "sample-project/api/pageBuilder", "sample-project/api/security/*" ], diff --git a/packages/api-cookie-policy/package.json b/packages/api-cookie-policy/package.json index 9048b255653..4cc27892e44 100644 --- a/packages/api-cookie-policy/package.json +++ b/packages/api-cookie-policy/package.json @@ -24,7 +24,8 @@ "@babel/core": "^7.5.5", "@babel/preset-env": "^7.5.5", "@babel/preset-typescript": "^7.8.3", - "@svgr/webpack": "^4.3.2" + "@svgr/webpack": "^4.3.2", + "@webiny/api-settings-manager": "^4.0.0-beta.1" }, "publishConfig": { "access": "public", diff --git a/packages/api-cookie-policy/src/index.ts b/packages/api-cookie-policy/src/index.ts index 7e0bf831810..41d57ce0a49 100644 --- a/packages/api-cookie-policy/src/index.ts +++ b/packages/api-cookie-policy/src/index.ts @@ -1,7 +1,11 @@ import gql from "graphql-tag"; -import { emptyResolver, resolveGetSettings, resolveUpdateSettings } from "@webiny/commodo-graphql"; +import { emptyResolver, ErrorResponse, resolveUpdateSettings } from "@webiny/commodo-graphql"; import { hasScope } from "@webiny/api-security"; import cookiePolicySettings from "./cookiePolicySettings.model"; +import { Context } from "@webiny/graphql/types"; +import { Context as SettingsManagerContext } from "@webiny/api-settings-manager/types"; + +type SettingsContext = Context & SettingsManagerContext; export default () => [ { @@ -102,7 +106,14 @@ export default () => [ cookiePolicy: emptyResolver }, CookiePolicyQuery: { - getSettings: resolveGetSettings(({ models }) => models.CookiePolicySettings) + getSettings: async (_, args, context: SettingsContext) => { + try { + const data = await context.settingsManager.getSettings("cookie-policy"); + return { data }; + } catch (err) { + return new ErrorResponse(err); + } + } }, CookiePolicyMutation: { updateSettings: hasScope("pb:settings")( diff --git a/packages/api-cookie-policy/tsconfig.build.json b/packages/api-cookie-policy/tsconfig.build.json index 83e612b14a8..a601603d940 100644 --- a/packages/api-cookie-policy/tsconfig.build.json +++ b/packages/api-cookie-policy/tsconfig.build.json @@ -4,6 +4,7 @@ "exclude": [ "node_modules", "../api-security", + "../api-settings-manager", "../commodo", "../commodo-graphql", "../validation" @@ -15,6 +16,7 @@ }, "references": [ { "path": "../api-security/tsconfig.build.json" }, + { "path": "../api-settings-manager/tsconfig.build.json" }, { "path": "../commodo-graphql/tsconfig.build.json" }, { "path": "../validation/tsconfig.build.json" } ] diff --git a/packages/api-cookie-policy/tsconfig.json b/packages/api-cookie-policy/tsconfig.json index a69e2c281f0..efcfa458d4f 100644 --- a/packages/api-cookie-policy/tsconfig.json +++ b/packages/api-cookie-policy/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../../tsconfig", "references": [ { "path": "../api-security" }, + { "path": "../api-settings-manager" }, { "path": "../commodo" }, { "path": "../commodo-graphql" }, { "path": "../validation" } diff --git a/packages/api-form-builder/package.json b/packages/api-form-builder/package.json index 80139916abb..f165cae34a4 100644 --- a/packages/api-form-builder/package.json +++ b/packages/api-form-builder/package.json @@ -39,6 +39,7 @@ "@babel/preset-env": "^7.5.5", "@babel/preset-typescript": "^7.8.3", "@webiny/api-plugin-commodo-db-proxy": "^4.0.0-beta.1", + "@webiny/api-settings-manager": "^4.0.0-beta.1", "flow-copy-source": "^2.0.2" }, "publishConfig": { diff --git a/packages/api-form-builder/src/plugins/graphql/formsSettings.ts b/packages/api-form-builder/src/plugins/graphql/formsSettings.ts index cdeec588e57..bd57105bf0a 100644 --- a/packages/api-form-builder/src/plugins/graphql/formsSettings.ts +++ b/packages/api-form-builder/src/plugins/graphql/formsSettings.ts @@ -1,5 +1,9 @@ -import { resolveUpdateSettings, resolveGetSettings } from "@webiny/commodo-graphql"; +import { resolveUpdateSettings, ErrorResponse } from "@webiny/commodo-graphql"; import { hasScope } from "@webiny/api-security"; +import { Context } from "@webiny/graphql/types"; +import { Context as SettingsManagerContext } from "@webiny/api-settings-manager/types"; + +type SettingsContext = Context & SettingsManagerContext; const getFormSettings = ctx => ctx.models.FormSettings; @@ -43,7 +47,14 @@ export default { `, resolvers: { FormsQuery: { - getSettings: hasScope("forms:settings")(resolveGetSettings(getFormSettings)) + getSettings: hasScope("forms:settings")(async (_, args, context: SettingsContext) => { + try { + const data = await context.settingsManager.getSettings("forms"); + return { data }; + } catch (err) { + return new ErrorResponse(err); + } + }) }, FormsMutation: { updateSettings: hasScope("forms:settings")(resolveUpdateSettings(getFormSettings)) diff --git a/packages/api-form-builder/tsconfig.build.json b/packages/api-form-builder/tsconfig.build.json index 38d7bda25d0..e8b8e6e56e8 100644 --- a/packages/api-form-builder/tsconfig.build.json +++ b/packages/api-form-builder/tsconfig.build.json @@ -6,6 +6,7 @@ "../graphql", "../api-i18n", "../api-security", + "../api-settings-manager", "../commodo", "../commodo-graphql", "../handler-ssr", @@ -20,6 +21,7 @@ { "path": "../graphql/tsconfig.build.json" }, { "path": "../api-i18n/tsconfig.build.json" }, { "path": "../api-security/tsconfig.build.json" }, + { "path": "../api-settings-manager/tsconfig.build.json" }, { "path": "../commodo-graphql/tsconfig.build.json" }, { "path": "../handler-ssr/tsconfig.build.json" }, { "path": "../validation/tsconfig.build.json" } diff --git a/packages/api-form-builder/tsconfig.json b/packages/api-form-builder/tsconfig.json index 28d46be28d8..3fed6c9d247 100644 --- a/packages/api-form-builder/tsconfig.json +++ b/packages/api-form-builder/tsconfig.json @@ -4,6 +4,7 @@ { "path": "../graphql" }, { "path": "../api-i18n" }, { "path": "../api-security" }, + { "path": "../api-settings-manager" }, { "path": "../commodo" }, { "path": "../commodo-graphql" }, { "path": "../handler-ssr" }, diff --git a/packages/api-google-tag-manager/package.json b/packages/api-google-tag-manager/package.json index ae8e11c588f..a3218eb9202 100644 --- a/packages/api-google-tag-manager/package.json +++ b/packages/api-google-tag-manager/package.json @@ -23,6 +23,7 @@ "@babel/core": "^7.5.5", "@babel/preset-env": "^7.5.5", "@babel/preset-typescript": "^7.8.3", + "@webiny/api-settings-manager": "^4.0.0-beta.1", "@svgr/webpack": "^4.3.2" }, "publishConfig": { diff --git a/packages/api-google-tag-manager/src/index.ts b/packages/api-google-tag-manager/src/index.ts index 03dc65bdb98..acd0b5b67c5 100644 --- a/packages/api-google-tag-manager/src/index.ts +++ b/packages/api-google-tag-manager/src/index.ts @@ -1,7 +1,11 @@ import gql from "graphql-tag"; -import { emptyResolver, resolveUpdateSettings, resolveGetSettings } from "@webiny/commodo-graphql"; +import { emptyResolver, resolveUpdateSettings, ErrorResponse } from "@webiny/commodo-graphql"; import { hasScope } from "@webiny/api-security"; import googleTagManagerSettings from "./googleTagManagerSettings.model"; +import { Context } from "@webiny/graphql/types"; +import { Context as SettingsManagerContext } from "@webiny/api-settings-manager/types"; + +type SettingsContext = Context & SettingsManagerContext; export default () => [ { @@ -63,7 +67,14 @@ export default () => [ googleTagManager: emptyResolver }, GtmQuery: { - getSettings: resolveGetSettings(({ models }) => models.GoogleTagManagerSettings) + getSettings: async (_, args, context: SettingsContext) => { + try { + const data = await context.settingsManager.getSettings("google-tag-manager"); + return { data }; + } catch (err) { + return new ErrorResponse(err); + } + } }, GtmMutation: { updateSettings: hasScope("pb:settings")( diff --git a/packages/api-google-tag-manager/tsconfig.build.json b/packages/api-google-tag-manager/tsconfig.build.json index 0d9c59ab73a..9a376aec17d 100644 --- a/packages/api-google-tag-manager/tsconfig.build.json +++ b/packages/api-google-tag-manager/tsconfig.build.json @@ -1,7 +1,14 @@ { "extends": "../../tsconfig.build.json", "include": ["./src"], - "exclude": ["node_modules", "./dist", "../api-security", "../commodo", "../commodo-graphql"], + "exclude": [ + "node_modules", + "./dist", + "../api-security", + "../api-settings-manager", + "../commodo", + "../commodo-graphql" + ], "compilerOptions": { "rootDir": "./src", "outDir": "./dist", @@ -9,6 +16,7 @@ }, "references": [ { "path": "../api-security/tsconfig.build.json" }, + { "path": "../api-settings-manager/tsconfig.build.json" }, { "path": "../commodo-graphql/tsconfig.build.json" } ] } diff --git a/packages/api-google-tag-manager/tsconfig.json b/packages/api-google-tag-manager/tsconfig.json index 5c0070391ab..e1744261b23 100644 --- a/packages/api-google-tag-manager/tsconfig.json +++ b/packages/api-google-tag-manager/tsconfig.json @@ -2,6 +2,7 @@ "extends": "../../tsconfig", "references": [ { "path": "../api-security" }, + { "path": "../api-settings-manager" }, { "path": "../commodo" }, { "path": "../commodo-graphql" } ] diff --git a/packages/api-mailchimp/package.json b/packages/api-mailchimp/package.json index 48f6bbfcee1..6cd749d24ce 100644 --- a/packages/api-mailchimp/package.json +++ b/packages/api-mailchimp/package.json @@ -27,6 +27,7 @@ "@babel/preset-react": "^7.0.0", "@babel/preset-typescript": "^7.8.3", "@svgr/webpack": "^4.3.2", + "@webiny/api-settings-manager": "^4.0.0-beta.1", "apollo-server-lambda": "^2.9.6" }, "publishConfig": { diff --git a/packages/api-mailchimp/src/index.ts b/packages/api-mailchimp/src/index.ts index bf4c39e906b..6217e5ceebd 100644 --- a/packages/api-mailchimp/src/index.ts +++ b/packages/api-mailchimp/src/index.ts @@ -1,11 +1,14 @@ import gql from "graphql-tag"; -import { emptyResolver, resolveGetSettings, resolveUpdateSettings } from "@webiny/commodo-graphql"; +import { emptyResolver, resolveUpdateSettings } from "@webiny/commodo-graphql"; import { ListErrorResponse, ListResponse, ErrorResponse } from "@webiny/graphql"; import { hasScope } from "@webiny/api-security"; import mailchimpSettings from "./mailchimpSettings.model"; import MailchimpApi from "./MailchimpApi"; import { get } from "lodash"; -import { Context } from "@webiny/graphql/types"; +import { Context, ContextPlugin, GraphQLSchemaPlugin } from "@webiny/graphql/types"; +import { Context as SettingsManagerContext } from "@webiny/api-settings-manager/types"; + +type SettingsContext = Context & SettingsManagerContext; export default () => [ { @@ -14,7 +17,7 @@ export default () => [ apply({ models }) { models.MailchimpSettings = mailchimpSettings({ createBase: models.createBase }); } - }, + } as ContextPlugin, { name: "graphql-schema-mailchimp", type: "graphql-schema", @@ -124,7 +127,14 @@ export default () => [ } }, getSettings: hasScope("pb:settings")( - resolveGetSettings(({ models }) => models.MailchimpSettings) + async (_, args, context: SettingsContext) => { + try { + const data = await context.settingsManager.getSettings("mailchimp"); + return { data }; + } catch (err) { + return new ErrorResponse(err); + } + } ) }, MailchimpMutation: { @@ -180,5 +190,5 @@ export default () => [ } } } - } + } as GraphQLSchemaPlugin ]; diff --git a/packages/api-mailchimp/tsconfig.build.json b/packages/api-mailchimp/tsconfig.build.json index d147d399897..239be186378 100644 --- a/packages/api-mailchimp/tsconfig.build.json +++ b/packages/api-mailchimp/tsconfig.build.json @@ -1,7 +1,15 @@ { "extends": "../../tsconfig.build.json", "include": ["./src"], - "exclude": ["node_modules", "./dist", "../graphql", "../api-security", "../commodo", "../commodo-graphql"], + "exclude": [ + "node_modules", + "./dist", + "../graphql", + "../api-security", + "../api-settings-manager", + "../commodo", + "../commodo-graphql" + ], "compilerOptions": { "rootDir": "./src", "outDir": "./dist", @@ -10,6 +18,7 @@ "references": [ { "path": "../graphql/tsconfig.build.json" }, { "path": "../api-security/tsconfig.build.json" }, + { "path": "../api-settings-manager/tsconfig.build.json" }, { "path": "../commodo-graphql/tsconfig.build.json" } ] } diff --git a/packages/api-mailchimp/tsconfig.json b/packages/api-mailchimp/tsconfig.json index db7db802171..23de7c6629f 100644 --- a/packages/api-mailchimp/tsconfig.json +++ b/packages/api-mailchimp/tsconfig.json @@ -3,6 +3,7 @@ "references": [ { "path": "../graphql" }, { "path": "../api-security" }, + { "path": "../api-settings-manager" }, { "path": "../commodo" }, { "path": "../commodo-graphql" } ] diff --git a/packages/api-page-builder/package.json b/packages/api-page-builder/package.json index 025856f9da2..cb5774cda5c 100644 --- a/packages/api-page-builder/package.json +++ b/packages/api-page-builder/package.json @@ -46,6 +46,7 @@ "@babel/plugin-proposal-export-default-from": "^7.5.2", "@babel/preset-env": "^7.5.5", "@babel/preset-typescript": "^7.8.3", + "@webiny/api-settings-manager": "^4.0.0-beta.1", "flow-copy-source": "^2.0.2" }, "publishConfig": { diff --git a/packages/api-page-builder/src/plugins/graphql/Settings.ts b/packages/api-page-builder/src/plugins/graphql/Settings.ts index 0c188839263..1d3b8b9b27e 100644 --- a/packages/api-page-builder/src/plugins/graphql/Settings.ts +++ b/packages/api-page-builder/src/plugins/graphql/Settings.ts @@ -1,4 +1,8 @@ -import { resolveUpdateSettings, resolveGetSettings } from "@webiny/commodo-graphql"; +import { resolveUpdateSettings, ErrorResponse } from "@webiny/commodo-graphql"; +import { Context } from "@webiny/graphql/types"; +import { Context as SettingsManagerContext } from "@webiny/api-settings-manager/types"; + +type SettingsContext = Context & SettingsManagerContext; export default { name: "graphql-schema-settings-page-builder", @@ -81,7 +85,14 @@ export default { `, resolvers: { PbQuery: { - getSettings: resolveGetSettings(ctx => ctx.models.PbSettings) + getSettings: async (_, args, context: SettingsContext) => { + try { + const data = await context.settingsManager.getSettings("page-builder"); + return { data }; + } catch (err) { + return new ErrorResponse(err); + } + } }, PbMutation: { updateSettings: resolveUpdateSettings(ctx => ctx.models.PbSettings) diff --git a/packages/api-page-builder/tsconfig.build.json b/packages/api-page-builder/tsconfig.build.json index eeebe488463..c4cf3386678 100644 --- a/packages/api-page-builder/tsconfig.build.json +++ b/packages/api-page-builder/tsconfig.build.json @@ -5,6 +5,7 @@ "node_modules", "../graphql", "../api-security", + "../api-settings-manager", "../commodo", "../commodo-graphql", "../handler-ssr", @@ -18,6 +19,7 @@ "references": [ { "path": "../graphql/tsconfig.build.json" }, { "path": "../api-security/tsconfig.build.json" }, + { "path": "../api-settings-manager/tsconfig.build.json" }, { "path": "../commodo-graphql/tsconfig.build.json" }, { "path": "../handler-ssr/tsconfig.build.json" }, { "path": "../validation/tsconfig.build.json" } diff --git a/packages/api-page-builder/tsconfig.json b/packages/api-page-builder/tsconfig.json index 0e617027008..47a12cc4671 100644 --- a/packages/api-page-builder/tsconfig.json +++ b/packages/api-page-builder/tsconfig.json @@ -3,6 +3,7 @@ "references": [ { "path": "../graphql" }, { "path": "../api-security" }, + { "path": "../api-settings-manager" }, { "path": "../commodo" }, { "path": "../commodo-graphql" }, { "path": "../handler-ssr" }, diff --git a/packages/api-settings-manager/.babelrc.js b/packages/api-settings-manager/.babelrc.js new file mode 100644 index 00000000000..9830652149f --- /dev/null +++ b/packages/api-settings-manager/.babelrc.js @@ -0,0 +1 @@ +module.exports = require("../../.babel.node"); \ No newline at end of file diff --git a/packages/api-settings-manager/LICENSE b/packages/api-settings-manager/LICENSE new file mode 100644 index 00000000000..f772d04d4db --- /dev/null +++ b/packages/api-settings-manager/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) Webiny + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/packages/api-settings-manager/README.md b/packages/api-settings-manager/README.md new file mode 100644 index 00000000000..9b87f059036 --- /dev/null +++ b/packages/api-settings-manager/README.md @@ -0,0 +1 @@ +# @webiny/api-settings-manager diff --git a/packages/api-settings-manager/package.json b/packages/api-settings-manager/package.json new file mode 100644 index 00000000000..0ca1e9dcf83 --- /dev/null +++ b/packages/api-settings-manager/package.json @@ -0,0 +1,40 @@ +{ + "name": "@webiny/api-settings-manager", + "version": "4.0.0-beta.2", + "repository": { + "type": "git", + "url": "https://github.com/webiny/webiny-js.git", + "directory": "packages/api-settings-manager" + }, + "description": "A handler for managing system settings", + "contributors": [ + "Pavel Denisjuk " + ], + "license": "MIT", + "dependencies": { + "@webiny/handler": "^4.0.0-beta.1", + "@webiny/graphql": "^4.0.0-beta.1", + "aws-sdk": "^2.685.0" + }, + "devDependencies": { + "@babel/cli": "^7.5.5", + "@babel/core": "^7.5.5" + }, + "publishConfig": { + "access": "public", + "directory": "dist" + }, + "scripts": { + "build": "rimraf ./dist *.tsbuildinfo && babel src -d dist --source-maps --copy-files --extensions \".ts\"", + "watch": "babel src -d dist --source-maps --copy-files --extensions \".ts\" --watch", + "postbuild": "cp package.json LICENSE README.md dist/ && tsc -p tsconfig.build.json", + "prepublishOnly": "yarn build" + }, + "adio": { + "ignore": { + "src": [ + "aws-sdk" + ] + } + } +} diff --git a/packages/api-settings-manager/src/client/SettingsManagerClient.ts b/packages/api-settings-manager/src/client/SettingsManagerClient.ts new file mode 100644 index 00000000000..694a94d2e71 --- /dev/null +++ b/packages/api-settings-manager/src/client/SettingsManagerClient.ts @@ -0,0 +1,50 @@ +import LambdaClient from "aws-sdk/clients/lambda"; +import { Action } from "../types"; + +interface SettingsManagerOperation { + action: Action; + key: string; + data?: { [key: string]: any }; +} + +export class SettingsManagerClient { + settingsManagerFunction: string; + lambda: LambdaClient; + + constructor({ functionName }) { + this.settingsManagerFunction = functionName; + } + + private async invoke(operation: SettingsManagerOperation) { + if (!this.lambda) { + this.lambda = new LambdaClient({ region: process.env.AWS_REGION }); + } + + const { Payload } = await this.lambda + .invoke({ + FunctionName: this.settingsManagerFunction, + Payload: JSON.stringify(operation) + }) + .promise(); + + const { error, data } = JSON.parse(Payload as string); + + if (error) { + throw Error(error); + } + + return data; + } + + async getSettings(key: string) { + return await this.invoke({ action: "getSettings", key }); + } + + async saveSettings(key: string, data) { + await this.invoke({ action: "saveSettings", key, data }); + } + + async deleteSettings(key: string) { + await this.invoke({ action: "deleteSettings", key }); + } +} diff --git a/packages/api-settings-manager/src/client/index.ts b/packages/api-settings-manager/src/client/index.ts new file mode 100644 index 00000000000..bb35ec663d9 --- /dev/null +++ b/packages/api-settings-manager/src/client/index.ts @@ -0,0 +1,18 @@ +import { ContextPlugin } from "@webiny/graphql/types"; +import { SettingsManagerClient } from "./SettingsManagerClient"; + +interface Params { + functionName: string; +} + +export default ({ functionName }: Params): ContextPlugin => { + return { + type: "context", + name: "context-settings-manager", + apply(context) { + context.settingsManager = new SettingsManagerClient({ + functionName + }); + } + }; +}; \ No newline at end of file diff --git a/packages/api-settings-manager/src/handler/index.ts b/packages/api-settings-manager/src/handler/index.ts new file mode 100644 index 00000000000..a0d96985de5 --- /dev/null +++ b/packages/api-settings-manager/src/handler/index.ts @@ -0,0 +1,47 @@ +import { HandlerPlugin } from "@webiny/handler/types"; +import { applyContextPlugins } from "@webiny/graphql"; +import { Action } from "../types"; + +interface ExecuteAction { + action: Action; + key: string; + data?: { [key: string]: any }; +} + +async function executeAction({ action, key, data }: ExecuteAction, driver) { + const options = { query: { key, deleted: false } }; + switch (action) { + case "getSettings": + const settings = await driver.findOne({ name: "Settings", options }); + return settings.data; + case "saveSettings": + await driver.update([{ name: "Settings", ...options, data }]); + return true; + case "deleteSettings": + await driver.delete([{ name: "Settings", ...options }]); + return true; + } +} + +export default (): HandlerPlugin => ({ + type: "handler", + name: "handler-settings-manager", + async handle({ args, context }) { + const [event] = args; + + await applyContextPlugins(context); + + try { + const data = await executeAction(event, context.commodo.driver); + return { error: false, data }; + } catch (err) { + console.log(err); + return { + error: { + message: err.message + }, + data: null + }; + } + } +}); diff --git a/packages/api-settings-manager/src/types.ts b/packages/api-settings-manager/src/types.ts new file mode 100644 index 00000000000..d5baf9778aa --- /dev/null +++ b/packages/api-settings-manager/src/types.ts @@ -0,0 +1,7 @@ +import { SettingsManagerClient } from "@webiny/api-settings-manager/client/SettingsManagerClient"; + +export type Action = "getSettings" | "saveSettings" | "deleteSettings"; + +export type Context = { + settingsManager: SettingsManagerClient; +}; diff --git a/packages/api-settings-manager/tsconfig.build.json b/packages/api-settings-manager/tsconfig.build.json new file mode 100644 index 00000000000..1087d0e1995 --- /dev/null +++ b/packages/api-settings-manager/tsconfig.build.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.build.json", + "compilerOptions": { + "rootDir": "./src", + "outDir": "./dist", + "declarationDir": "./dist" + }, + "exclude": ["node_modules", "dist", "../graphql", "../handler"], + "references": [ + { "path": "../handler/tsconfig.build.json" }, + { "path": "../graphql/tsconfig.build.json" } + ] +} diff --git a/packages/api-settings-manager/tsconfig.json b/packages/api-settings-manager/tsconfig.json new file mode 100644 index 00000000000..2f705425eb0 --- /dev/null +++ b/packages/api-settings-manager/tsconfig.json @@ -0,0 +1,4 @@ +{ + "extends": "../../tsconfig", + "references": [{ "path": "../handler" }, { "path": "../graphql" }] +} diff --git a/tsconfig.build.json b/tsconfig.build.json index 78079298236..9a6c96764e0 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -31,6 +31,7 @@ "@webiny/api-plugin-commodo-db-proxy": ["./packages/api-plugin-commodo-db-proxy/src"], "@webiny/api-plugin-commodo-mongodb/*": ["./packages/api-plugin-commodo-mongodb/src/*"], "@webiny/api-plugin-commodo-mongodb": ["./packages/api-plugin-commodo-mongodb/src"], + "@webiny/api-settings-manager/*": ["./packages/api-settings-manager/src/*"], "@webiny/api-security/*": ["./packages/api-security/src/*"], "@webiny/api-security": ["./packages/api-security/src"], "@webiny/app/*": ["./packages/app/src/*"],