Skip to content

Commit

Permalink
feat: Init lowdefy-example-plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Jun 28, 2022
0 parents commit 3becfb6
Show file tree
Hide file tree
Showing 28 changed files with 428 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Lowdefy

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.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Lowdefy Plugins Example

A plugin needs to be a npm package. The package is installed into the server using npm or yarn, so any of the supported protocols like `file:` or `git:` should work. We copy the `plugins` folder in your config into the server, so you can use relative paths there in the package specifier if you place your plugins in the `plugins` folder.

You can have a look at the official Lowdefy plugins here:
https://github.com/lowdefy/lowdefy/tree/main/packages/plugins
The official plugins are built using swc, but is not really necessary, as the Lowdefy server is built with webpack, so it is simpler not to include a build step.

To import the plugin into your app, you can add the following to the root of your lowdefy config (replacing the `types` array):

```yaml
plugins:
- name: 'local-plugin'
version: 'file:./plugins/local-plugin'
- name: 'npm-plugin'
version: 1.0.0
```
You can then use all of the types declared by the plugin as normal types.
The plugin `package,json` file should look like this:
```json
{
"name": "plugin-name",
"version": "1.0.0",
"type": "module",
"exports": {
"./auth/callbacks": "./auth/callbacks.js",
"./auth/events": "./auth/events.js",
"./auth/providers": "./auth/providers.js",
"./blocks": "./blocks.js",
"./connections": "./connections.js",
"./operators/build": "./operatorsBuild.js",
"./operators/client": "./operatorsClient.js",
"./operators/server": "./operatorsServer.js",
"./types": "./types.js"
},
"files": [
"*"
],
"dependencies": {}
}
```

You can change your folder structure in your package, but then you need to change the `exports` config to match your file structure.

You should have a `types.js` file, which exports all the types declared by the package. This is used so that the build knows which types can be used from that plugin. The type names exported in the `types.js` are the type names you can use in your Lowdefy config.

The `types.js` file should look like

```js
export default {
actions: ['Action'],
auth: {
callbacks: ['Callback'],
events: ['Event'],
provider: ['Provider']
},
blocks: ['Block'],
connections: ['Connection'],
requests: ['Request'],
operators: {
client: ['_build_operator', '_shared_operator'],
client: ['_client_operator', '_shared_operator'],
server: ['_server_operator', '_shared_operator'],
},
};
```

The plugin should also have a file that exports all the types as named exports. So for example, you should have a `blocks.js`:

```js
export { default as Block } from './blocks/Block/Block.js';
```

The block file also needs to change slightly, an example block is

```js
import React from 'react';
import { blockDefaultProps } from '@lowdefy/block-utils';
const Box = ({ blockId, content, events, methods, properties }) => (
<div
id={blockId}
data-testid={blockId}
onClick={() => methods.triggerEvent({ name: 'onClick' })}
className={methods.makeCssClass([
{ outline: 'none', cursor: events.onClick && 'pointer' },
properties.style,
])}
>
{properties.content || (content.content && content.content())}
</div>
);
Box.defaultProps = blockDefaultProps;
Box.meta = {
category: 'container', // one of 'container', 'display', 'input' or 'list'
icons: [],
styles: [],
};
export default Block;
```
6 changes: 6 additions & 0 deletions lowdefy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lowdefy: 4.0.0-alpha.17
license: MIT

plugins:
- name: 'plugin-name'
version: 'file:./plugin-name'
1 change: 1 addition & 0 deletions plugins/plugin-name/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Action } from './actions/Action.js';
8 changes: 8 additions & 0 deletions plugins/plugin-name/actions/Action.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

// See https://github.com/lowdefy/lowdefy/tree/main/packages/engine/src/actions/getActionMethods.js
// for available methods
function Action({ globals, methods, params }) {

}

export default Action;
1 change: 1 addition & 0 deletions plugins/plugin-name/auth/callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Callback } from './callbacks/Callback.js';
11 changes: 11 additions & 0 deletions plugins/plugin-name/auth/callbacks/Callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// See https://next-auth.js.org/configuration/callbacks for more info

function Callback({ account, isNewUser, profile, properties, token, user }) {
return token;
}

Callback.meta = {
type: 'jwt', // One of jwt, session, redirect or signIn
};

export default Callback;
1 change: 1 addition & 0 deletions plugins/plugin-name/auth/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Event } from './events/Event.js';
11 changes: 11 additions & 0 deletions plugins/plugin-name/auth/events/Event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// See https://next-auth.js.org/configuration/events for more info

function Event({}) {
return token;
}

Event.meta = {
type: 'createUser', // One of createUser, linkAccount, session, signIn, signOut, updateUser
};

export default Event;
1 change: 1 addition & 0 deletions plugins/plugin-name/auth/providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Provider } from './providers/Provider.js';
18 changes: 18 additions & 0 deletions plugins/plugin-name/auth/providers/Provider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// See https://next-auth.js.org
export default {
id: "google",
name: "Google",
type: "oauth",
wellKnown: "https://accounts.google.com/.well-known/openid-configuration",
authorization: { params: { scope: "openid email profile" } },
idToken: true,
checks: ["pkce", "state"],
profile(profile) {
return {
id: profile.sub,
name: profile.name,
email: profile.email,
image: profile.picture,
}
},
}
1 change: 1 addition & 0 deletions plugins/plugin-name/blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Block } from './blocks/Block/Block.js';
28 changes: 28 additions & 0 deletions plugins/plugin-name/blocks/Block/Block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// See https://github.com/lowdefy/lowdefy/tree/main/packages/plugins/blocks/blocks-basic
// for more examples


import React from 'react';
import { blockDefaultProps } from '@lowdefy/block-utils';

const Block = ({ basePath, blockId, components, content, events, list, loading, methods, menus, pageId, properties, required, validation, value }) => (
<div
id={blockId}
onClick={() => methods.triggerEvent({ name: 'onClick' })}
className={methods.makeCssClass([
{ outline: 'none', cursor: events.onClick && 'pointer' },
properties.style,
])}
>
{properties.content || (content.content && content.content())}
</div>
);

Box.defaultProps = blockDefaultProps;
Box.meta = {
category: 'display', // one of 'container', 'display', 'input' or 'list'
icons: [],
styles: [],
};

export default Box;
30 changes: 30 additions & 0 deletions plugins/plugin-name/blocks/Block/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "object",
"properties": {
"type": "object",
"additionalProperties": false,
"properties": {
"content": {
"type": "string",
"description": "Block content string. Overrides the \"content\" content area."
},
"style": {
"type": "object",
"description": "Css style object to apply to Block div.",
"docs": {
"displayType": "yaml"
}
}
}
},
"events": {
"type": "object",
"additionalProperties": false,
"properties": {
"onClick": {
"type": "array",
"description": "Trigger actions when the Block is clicked."
}
}
}
}
1 change: 1 addition & 0 deletions plugins/plugin-name/connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Connection } from './actions/Connection.js';
9 changes: 9 additions & 0 deletions plugins/plugin-name/connections/Connection/Connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Request from './Request/Request.js';
import schema from './schema.js';

export default {
schema,
requests: {
Request,
},
};
13 changes: 13 additions & 0 deletions plugins/plugin-name/connections/Connection/Request/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import schema from '../schema.js';

async function Request({ request, connection }) {
return null
}

Request.schema = schema;
Request.meta = {
checkRead: false,
checkWrite: false,
};

export default Request;
5 changes: 5 additions & 0 deletions plugins/plugin-name/connections/Connection/Request/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Request Schema',
type: 'object',
};
5 changes: 5 additions & 0 deletions plugins/plugin-name/connections/Connection/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Connection Schema',
type: 'object',
};
7 changes: 7 additions & 0 deletions plugins/plugin-name/operators/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import _build_operator from './operators/build/_build_operator.js';
import _shared_operator from './operators/shared/_shared_operator.js';

export {
_build_operator,
_shared_operator
}
18 changes: 18 additions & 0 deletions plugins/plugin-name/operators/build/_build_operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function _build_operator({
args,
arrayIndices,
env,
location,
methodName,
operators,
params,
operatorPrefix,
parser,
payload,
runtime,
user,
}) {
return null;
}

export default _build_operator;
7 changes: 7 additions & 0 deletions plugins/plugin-name/operators/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import _client_operator from './operators/client/_client_operator.js';
import _shared_operator from './operators/shared/_shared_operator.js';

export {
_client_operator,
_shared_operator
}
29 changes: 29 additions & 0 deletions plugins/plugin-name/operators/client/_client_operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function _client_operator({
actions,
args,
arrayIndices,
basePath,
event,
eventLog,
globals,
home,
input,
location,
lowdefyGlobal,
menus,
methodName,
operatorPrefix,
operators,
pageId,
params,
parser,
requests,
runtime,
state,
urlQuery,
user,
}) {
return null;
}

export default _client_operator;
7 changes: 7 additions & 0 deletions plugins/plugin-name/operators/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import _server_operator from './operators/server/_build_operator.js';
import _shared_operator from './operators/shared/_shared_operator.js';

export {
_server_operator,
_shared_operator
}
18 changes: 18 additions & 0 deletions plugins/plugin-name/operators/server/_server_operator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function _server_operator({
args,
arrayIndices,
env,
location,
methodName,
operators,
params,
operatorPrefix,
parser,
payload,
runtime,
user,
}) {
return null;
}

export default _server_operator;
Loading

0 comments on commit 3becfb6

Please sign in to comment.