Skip to content

Commit

Permalink
fix(module): typings for sdk, knex & router (#2995)
Browse files Browse the repository at this point in the history
  • Loading branch information
allardy committed Feb 25, 2020
1 parent 93efff2 commit fc61430
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 35 deletions.
25 changes: 5 additions & 20 deletions build/gulp.modules.js
Expand Up @@ -29,21 +29,6 @@ const readModuleConfig = modulePath => {
return JSON.parse(fs.readFileSync(packagePath))
}

/**
* Copies the [`botpress.d.ts`]{@see ../src/bp/sdk/botpress.d.ts} file to all the
* modules individually.
*/
const copySdkDefinitions = () => {
let stream = gulp.src(['src/bp/sdk/botpress.d.ts', 'src/typings/global.d.ts'])
const modules = getAllModulesRoot()
for (let m of modules) {
const src = _.get(readModuleConfig(m), 'botpress.src', 'src')
const dest = path.join(m, src)
stream = stream.pipe(gulp.dest(dest)).pipe(print())
}
return stream
}

const getTargetOSConfig = () => {
if (process.argv.find(x => x.toLowerCase() === '--win32')) {
return 'win32'
Expand Down Expand Up @@ -142,12 +127,13 @@ const packageModules = () => {
return gulp.series(tasks)
}

const build = () => {
return gulp.series([buildModuleBuilder, copySdkDefinitions, buildModules()])
// Temporarily cleaning the sdk, otherwise local copies will generate an error
const cleanSdk = () => {
return gulp.src(['./modules/*/src/global.d.ts', './modules/*/src/botpress.d.ts'], { allowEmpty: true }).pipe(rimraf())
}

const buildSdk = () => {
return gulp.series([copySdkDefinitions])
const build = () => {
return gulp.series([buildModuleBuilder, cleanSdk, buildModules()])
}

const cleanModuleAssets = () => {
Expand Down Expand Up @@ -190,7 +176,6 @@ const createAllModulesSymlink = () => {

module.exports = {
build,
buildSdk,
buildModules,
packageModules,
buildModuleBuilder,
Expand Down
1 change: 0 additions & 1 deletion gulpfile.js
Expand Up @@ -40,7 +40,6 @@ gulp.task('build:ui', ui.build())
gulp.task('build:core', core.build())
gulp.task('build:shared', ui.buildShared())
gulp.task('build:modules', gulp.series([modules.build()]))
gulp.task('build:sdk', gulp.series([modules.buildSdk()]))

gulp.task('start:guide', docs.startDevServer)
gulp.task('build:guide', docs.buildGuide())
Expand Down
8 changes: 7 additions & 1 deletion modules/code-editor/build.extras.js
@@ -1,9 +1,15 @@
module.exports = {
copyFiles: [
'src/botpress.d.ts',
'src/typings/node.d.ts',
'src/typings/node.d.txt',
'src/typings/es6include.txt',
'src/typings/bot.config.schema.json',
'src/typings/botpress.config.schema.json'
]
}

const fs = require('fs')
const path = require('path')

fs.mkdirSync('dist')
fs.copyFileSync(path.join(__dirname, '../../src/bp/sdk/botpress.d.ts'), path.join(__dirname, 'dist/botpress.d.js'))
2 changes: 1 addition & 1 deletion modules/code-editor/src/backend/editor.ts
Expand Up @@ -178,7 +178,7 @@ export default class Editor {
}

const sdkTyping = fs.readFileSync(path.join(__dirname, '/../botpress.d.js'), 'utf-8')
const nodeTyping = fs.readFileSync(path.join(__dirname, `/../typings/node.d.js`), 'utf-8')
const nodeTyping = fs.readFileSync(path.join(__dirname, `/../typings/node.d.txt`), 'utf-8')

const ghost = this.bp.ghost.forRoot()
const botConfigSchema = await ghost.readFileAsString('/', 'bot.config.schema.json')
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions modules/global.d.ts
@@ -0,0 +1 @@
/// <reference path="../src/typings/global.d.ts" /> #
10 changes: 6 additions & 4 deletions modules/tsconfig.shared.json
Expand Up @@ -12,11 +12,13 @@
"moduleResolution": "node",
"target": "es6",
"lib": ["es7"],
"typeRoots": ["./node_modules/@types", "./node_modules", "./src/botpress.d.ts", "./src/typings"],
"types": ["reflect-metadata", "jest", "bluebird-global", "node"],
"typeRoots": ["./node_modules/@types", "./node_modules", "./src/typings"],
"types": ["reflect-metadata", "jest", "bluebird-global"],
"paths": {
"botpress/common/typings": ["../../out/bp/common/typings.d.ts"]
"botpress/common/typings": ["../../out/bp/common/typings.d.ts"],
"botpress/sdk": ["../../src/bp/sdk/botpress.d.ts"]
}
},
"exclude": ["**/*.test.ts", "./src/views/**"]
"exclude": ["**/*.test.ts", "./src/views/**"],
"include": ["**/*.ts", "../../src/typings/global.d.ts"]
}
2 changes: 1 addition & 1 deletion modules/tsconfig_view.shared.json
Expand Up @@ -15,7 +15,7 @@
"botpress/ui": ["../../../../src/bp/ui-studio/src/web/components/Shared/Interface/typings.d.ts"],
"botpress/utils": ["../../../../src/bp/ui-studio/src/web/components/Shared/Utils/typings.d.ts"],
"botpress/shared": ["../../../../src/bp/ui-shared/src/Interface/typings.d.ts"],
"botpress/sdk": ["../botpress.d.ts"],
"botpress/sdk": ["../../../../src/bp/sdk/botpress.d.ts"],
"botpress/common/typings": ["../../../../out/bp/common/typings.d.ts"],
"@blueprintjs/core": ["../../../../src/bp/ui-studio/node_modules/@blueprintjs/core"],
"@blueprintjs/select": ["../../../../src/bp/ui-studio/node_modules/@blueprintjs/select"],
Expand Down
2 changes: 1 addition & 1 deletion src/bp/core/routers/bots/index.ts
Expand Up @@ -128,7 +128,7 @@ export class BotsRouter extends CustomRouter {
}
}

getNewRouter(path: string, identity: string, options?: RouterOptions) {
getNewRouter(path: string, identity: string, options?: RouterOptions): Router {
const router = Router({ mergeParams: true })
if (_.get(options, 'checkAuthentication', true)) {
router.use(this.checkTokenHeader)
Expand Down
11 changes: 5 additions & 6 deletions src/bp/sdk/botpress.d.ts
Expand Up @@ -6,6 +6,7 @@
*/
declare module 'botpress/sdk' {
import Knex from 'knex'
import { Router, Request, Response, NextFunction } from 'express'

export interface KnexExtension {
isLite: boolean
Expand Down Expand Up @@ -1313,7 +1314,7 @@ declare module 'botpress/sdk' {
* @param options - Additional options to apply to the router
* @param router - The router
*/
export function createRouterForBot(routerName: string, options?: RouterOptions): any & RouterExtension
export function createRouterForBot(routerName: string, options?: RouterOptions): RouterExtension

/**
* This method is meant to unregister a router before unloading a module. It is meant to be used in a development environment.
Expand All @@ -1339,18 +1340,16 @@ declare module 'botpress/sdk' {
/**
* This Express middleware tries to decode the X-BP-ExternalAuth header and adds a credentials header in the request if it's valid.
*/
export function extractExternalToken(req: any, res: any, next: any): Promise<void>
export function extractExternalToken(req: Request, res: Response, next: NextFunction): Promise<void>

export function needPermission(
operation: string,
resource: string
): (req: any, res: any, next: any) => Promise<void>
): (req: Request, res: Response, next: NextFunction) => Promise<void>

export function hasPermission(req: any, operation: string, resource: string): Promise<boolean>

export interface RouterExtension {
getPublicPath(): Promise<string>
}
export type RouterExtension = { getPublicPath(): Promise<string> } & Router
}

/**
Expand Down

0 comments on commit fc61430

Please sign in to comment.