Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] add plugin-helper for plugin type definition #1999

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/plugin-helper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@waline/plugin-helper",
"version": "1.0.0",
"description": "Waline plugin helper",
"scripts": {
"clean": "rimraf ./dist",
"prepublishOnly": "pnpm clean && pnpm build",
"build": "rollup -c"
},
"keywords": [
"waline",
"plugin",
"builder",
"helper"
],
"repository": {
"url": "https://github.com/walinejs/waline",
"directory": "packages/plugin-helper"
},
"license": "MIT",
"author": {
"name": "lizheming",
"email": "i@imnerd.org",
"url": "https://imnerd.org"
},
"main": "./dist/index.js",
"types": "./dist/type.d.ts",
"publishConfig": {
"provenance": true
},
"engines": {
"node": ">=14"
},
"devDependencies": {
"@types/koa": "^2.13.6",
"@waline/client": "workspace:^2.15.5",
"dittorm": "^1.0.0",
"koa": "^2.14.2",
"rollup": "3.23.0",
"rollup-plugin-ts": "3.2.0",
"thinkjs": "^3.2.14",
"typescript": "5.0.4"
}
}
27 changes: 27 additions & 0 deletions packages/plugin-helper/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ts from 'rollup-plugin-ts';
import dts from 'rollup-plugin-dts';

export default [
{
input: './src/index.ts',
output: {
file: './dist/index.js',
format: 'cjs'
},
plugins: [
ts()
],
treeshake: 'smallest',
},
{
input: 'src/index.ts',
output: {
file: './dist/type.d.ts',
format: 'esm'
},
plugins: [
dts({ compilerOptions: { preserveSymlinks: false } })
],
}
];

46 changes: 46 additions & 0 deletions packages/plugin-helper/src/hook.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { type WalineCommentData } from '@waline/client';

export interface PreSaveHook {
name: 'preSave';
callback: (data: WalineCommentData) => void;
}

export interface PostSaveHook {
name: 'postSave';
callback: (
comment: WalineCommentData,
parentComment?: WalineCommentData
) => void;
}

export interface PreUpdateHook {
name: 'preUpdate';
callback: (data: WalineCommentData) => void;
}

export interface PostUpdateHook {
name: 'postUpdate';
callback: (data: WalineCommentData) => void;
}

export interface PreDeleteHook {
name: 'preDelete';
callback: (id: string | number) => void;
}

export interface PostDeleteHook {
name: 'postDelete';
callback: (id: string | number) => void;
}

export type Hooks =
| PreSaveHook
| PostSaveHook
| PreUpdateHook
| PostUpdateHook
| PreDeleteHook
| PostDeleteHook;

type PropertyType<T, K extends keyof T> = T extends any ? T[K] : never;

Check warning on line 44 in packages/plugin-helper/src/hook.d.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 44 in packages/plugin-helper/src/hook.d.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
export type HooksName = PropertyType<Hooks, 'name'>;
export type HooksCallback = PropertyType<Hooks, 'callback'>;
27 changes: 27 additions & 0 deletions packages/plugin-helper/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Hooks, HooksCallback, HooksName } from './hook';
import { Middleware } from './middleware';

export interface WalinePlugin {
hooks: Partial<Record<HooksName, HooksCallback>>;
middlewares: Middleware[];
}

export default class PluginHelper {
hooks: WalinePlugin['hooks'] = {};
middlewares: WalinePlugin['middlewares'] = [];

hook(options: Hooks): void {
this.hooks[options.name] = options.callback;
}

middleware(fn: Middleware): void {
this.middlewares.push(fn);
}

valueOf(): WalinePlugin {
return {
hooks: this.hooks,
middlewares: this.middlewares,
};
}
}
23 changes: 23 additions & 0 deletions packages/plugin-helper/src/middleware.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { type UserInfo as ClientUserInfo } from '@waline/client/api';
import type Storage from 'dittorm/dist/src/storage/base';
import { type Middleware as KoaMiddleware } from 'koa';
import { type Context as ThinkJSContext } from 'thinkjs';

interface UserInfo extends ClientUserInfo {
password: string;
}

export interface WalineContextState {
deprecated?: boolean;
userInfo?: UserInfo;
token?: string;
}

export interface WalineContext extends ThinkJSContext {
serverURL: string;
webhook: (type: string, data: Record<string, unknown>) => void;
getModel: (modelName: string) => Storage<any>;

Check warning on line 19 in packages/plugin-helper/src/middleware.d.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 19 in packages/plugin-helper/src/middleware.d.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
locale: (message: string, variables: Record<string, string>) => string;
}

export type Middleware = KoaMiddleware<WalineContextState, WalineContext>;
13 changes: 13 additions & 0 deletions packages/plugin-helper/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"noImplicitAny": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"jsx": "preserve",
"module": "ESNext",
"moduleResolution": "nodenext",
"rootDir": "./src"
},
"include": ["./src/**/*.ts"]
}
29 changes: 29 additions & 0 deletions packages/server/src/extend/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const nunjucks = require('nunjucks');

const locales = require('../locales');

module.exports = {
locale(message, variables) {
const { lang } = this.get();
const locale = locales[(lang || 'zh-cn').toLowerCase()] || locales['zh-cn'];

if (locale && locale[message]) {
message = locale[message];
}

return nunjucks.renderString(message, variables);
},
getModel(modelName) {
const { storage, model } = this.config();

if (typeof model === 'function') {
const modelInstance = model(modelName, this);

if (modelInstance) {
return modelInstance;
}
}

return this.service(`storage/${storage}`, modelName);
},
};
24 changes: 2 additions & 22 deletions packages/server/src/extend/controller.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
const nunjucks = require('nunjucks');
const { PasswordHash } = require('phpass');

const locales = require('../locales');

module.exports = {
success(...args) {
this.ctx.success(...args);
Expand All @@ -18,27 +15,10 @@ module.exports = {
return this[this.ctx.state.deprecated ? 'json' : 'success'](...args);
},
locale(message, variables) {
const { lang } = this.get();
const locale = locales[(lang || 'zh-cn').toLowerCase()] || locales['zh-cn'];

if (locale && locale[message]) {
message = locale[message];
}

return nunjucks.renderString(message, variables);
return this.ctx.locale(message, variables);
},
getModel(modelName) {
const { storage, model } = this.config();

if (typeof model === 'function') {
const modelInstance = model(modelName, this);

if (modelInstance) {
return modelInstance;
}
}

return this.service(`storage/${storage}`, modelName);
return this.ctx.getModel(modelName);
},
hashPassword(password) {
const PwdHash = this.config('encryptPassword') || PasswordHash;
Expand Down
Loading
Loading