@ablula/egg-yuque-viewer
Advanced tools
Comparing version 0.2.0-beta.0 to 0.2.0-beta.1
'use strict'; | ||
const assert = require('assert'); | ||
const { deepMerge } = require('./utils'); | ||
const BASE_URL = 'https://www.yuque.com/api/v2'; | ||
@@ -68,3 +68,4 @@ | ||
mergeConfig(config) { | ||
this.config = { ...this.config, ...(config || {}) }; | ||
this.config = deepMerge(this.config, config); | ||
this.baseUrl = config.baseUrl || BASE_URL; | ||
} | ||
@@ -254,3 +255,3 @@ | ||
if (result.status !== 200 || !result.data) { | ||
throw new Error(`Search document from lark failed, url: ${url}`); | ||
throw new Error(`request document from lark failed, url: ${url}`); | ||
} | ||
@@ -257,0 +258,0 @@ return result.data; |
'use strict'; | ||
const { deepMerge } = require('./utils'); | ||
const YuqueClient = require('./YuqueClient'); | ||
@@ -30,3 +31,3 @@ | ||
mergeConfig(config = {}) { | ||
this.config = { ...this.config, ...(config || {}) }; | ||
this.config = deepMerge(this.config, config); | ||
this.init(); | ||
@@ -57,3 +58,2 @@ } | ||
const { app } = this; | ||
const { router } = app; | ||
let view = config.view || ''; | ||
@@ -68,3 +68,3 @@ view = view.replace('/*', ''); | ||
if (showSearch && !search) { | ||
console.warn('show search without search api, search won\'t be shown'.yellow); | ||
app.logger.warn('[EggYuqueViewer] show search without search api, search won\'t be shown'.yellow); | ||
} | ||
@@ -75,18 +75,4 @@ if (lazyLoad && !prefix) { | ||
if (search) { | ||
router.get(`GetDocumentView-${search}`, `${search}`, async ctx => { | ||
const keywords = ctx.query.keywords; | ||
if (!keywords) { | ||
throw new Error('keywords param required.'); | ||
} | ||
const result = await yuqueClient.search(keywords); | ||
ctx.status = 200; | ||
ctx.body = result; | ||
}); | ||
router.stack.unshift(router.stack.pop()); | ||
} | ||
if (prefix) { | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${prefix}/*`); | ||
router.get('GetDocument', `${prefix}/*`, async (ctx, next) => { | ||
this.unshiftRoute('GetDocument', `${prefix}/*`, async (ctx, next) => { | ||
const locator = ctx.request.path.replace(`${prefix}/`, ''); | ||
@@ -110,25 +96,23 @@ if (!locator) { | ||
}); | ||
router.stack.unshift(router.stack.pop()); | ||
} | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${view}/index.js`); | ||
router.get(`Static Resources-${view}/js`, `${view}/index.js`, async ctx => { | ||
if (search) { | ||
this.unshiftRoute(`GetDocumentView-${search}`, `${search}`, async ctx => { | ||
const keywords = ctx.query.keywords; | ||
if (!keywords) { | ||
throw new Error('keywords param required.'); | ||
} | ||
const result = await yuqueClient.search(keywords); | ||
ctx.status = 200; | ||
ctx.body = result; | ||
}); | ||
} | ||
this.unshiftRoute(`Static Resources-${view}/js`, `${view}/index.js`, async ctx => { | ||
return ctx.redirect(`https://cdn.jsdelivr.net/npm/${config.npm}/build/index.js`); | ||
}); | ||
router.stack.unshift(router.stack.pop()); | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${view}/index.css`); | ||
router.get(`Static Resources-${view}/css`, `${view}/index.css`, async ctx => { | ||
this.unshiftRoute(`Static Resources-${view}/css`, `${view}/index.css`, async ctx => { | ||
return ctx.redirect(`https://cdn.jsdelivr.net/npm/${config.npm}/build/index.css`); | ||
}); | ||
router.stack.unshift(router.stack.pop()); | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${view}`); | ||
router.get(`GetDocumentView-${view}`, `${view}`, async (ctx, next) => { | ||
ctx.body = await yuqueClient.getDocumentView(); | ||
next(); | ||
}); | ||
router.stack.unshift(router.stack.pop()); | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${view}/*`); | ||
router.get(`GetDocumentView-${view}`, `${view}/*`, async (ctx, next) => { | ||
this.unshiftRoute(`GetDocumentView-${view}`, `${view}/*`, async (ctx, next) => { | ||
const subPath = ctx.request.path.replace(`${view}/`, ''); | ||
@@ -147,3 +131,14 @@ if (subPath.startsWith('http')) { | ||
}); | ||
this.unshiftRoute(`GetDocumentView-${view}`, `${view}`, async (ctx, next) => { | ||
ctx.body = await yuqueClient.getDocumentView(); | ||
next(); | ||
}); | ||
} | ||
unshiftRoute(name, path, handler, method = 'get') { | ||
const { app } = this; | ||
const { router } = app; | ||
router[method](name, path, handler); | ||
router.stack.unshift(router.stack.pop()); | ||
app.logger.info('[EggYuqueViewer] bind router: ', `${name} - ${path}`); | ||
} | ||
@@ -150,0 +145,0 @@ |
{ | ||
"name": "@ablula/egg-yuque-viewer", | ||
"author": "mark.ck", | ||
"version": "0.2.0-beta.0", | ||
"version": "0.2.0-beta.1", | ||
"description": "egg plugin for yuque viewer", | ||
@@ -6,0 +6,0 @@ "eggPlugin": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20260
11
509