Skip to content

Commit

Permalink
feat: implement typescript support, change file extension names to ts…
Browse files Browse the repository at this point in the history
…, fix few types
  • Loading branch information
fletcherist committed Jul 1, 2018
1 parent 1b4774c commit 6dd23b1
Show file tree
Hide file tree
Showing 24 changed files with 97 additions and 52 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
module.exports = {
extends: 'standard',
parser: 'typescript-eslint-parser',
parserOptions: {
sourceType: 'module',
ecmaFeatures: {
modules: true
}
},
rules: {
'no-return-await': 0,
'space-before-function-paren': ['error', {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
dist

# Runtime data
pids
Expand Down
4 changes: 2 additions & 2 deletions examples/aliceListen.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Alice = require('../src/index')
const Alice = require('../dist/index')
const alice = new Alice()

alice.welcome(async (ctx) => {
Expand All @@ -13,4 +13,4 @@ alice.any(async (ctx) => {
ctx.reply('О чём это вы?')
})

alice.listen('/', 8080)
alice.listen('/', 8080)
6 changes: 3 additions & 3 deletions examples/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Alice = require('../src/index')
const Alice = require('../dist/index')
const alice = new Alice()

/*
Expand All @@ -11,7 +11,7 @@ const alice = new Alice()
*
* Для строк и массивов строк использутся fuzzysearch (fuse.js)
* В случае с регулярными выражениями вы пишете триггер сами.
*
*
*/

alice.welcome(async (ctx) => {
Expand All @@ -38,4 +38,4 @@ alice.any(async (ctx) => {
ctx.reply('О чём это вы?')
})

alice.listen('/', 8080)
alice.listen('/', 8080)
4 changes: 1 addition & 3 deletions examples/ctxBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ alice.command('забронируй встречу в ${where} на ${when}', ct
ctx.reply(`Готово. Встреча состоится в ${where}. Тебе напомнить?`)
})


alice.command('начни готовить ${what}', ctx => {
const { what } = ctx.body // завтрак/ужин
ctx.reply('хорошо')
})


alice.handleRequestBody(generateRequest('забронируй встречу в 7-холмов на 18:00'))
alice.handleRequestBody(generateRequest('начни готовить завтрак'))
alice.handleRequestBody(generateRequest('начни готовить ужин'))
alice.handleRequestBody(generateRequest('начни готовить ужин'))
2 changes: 1 addition & 1 deletion examples/micro.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Для асинхронной работы используется пакет micro.
const { json } = require('micro')
const Alice = require('../src/index')
const Alice = require('../dist/index')
const alice = new Alice()

alice.command('say my name', (ctx) => {
Expand Down
6 changes: 3 additions & 3 deletions examples/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const Alice = require('../src/index')
const Alice = require('../dist/index')
const alice = new Alice()

const aliceCorsMiddleware = (params) => {
return ctx => {
if (ctx.server) {
ctx.server.use(function(req, res, next) {
ctx.server.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
next()
Expand All @@ -22,4 +22,4 @@ alice.command('дай совет', async (ctx) => {
})

const port = 8080
alice.listen('/', port).then(console.log('listening on:', port))
alice.listen('/', port).then(console.log('listening on:', port))
4 changes: 2 additions & 2 deletions examples/scenes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Alice = require('../src')
const Scene = require('../src').Scene
const Alice = require('../dist')
const Scene = require('../dist').Scene

const alice = new Alice()

Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
"name": "yandex-dialogs-sdk",
"version": "1.2.1",
"description": "Build your skill for Alice with ease.",
"main": "src/index.js",
"main": "dist/index.js",
"scripts": {
"lint": "eslint src",
"test": "jest",
"dev": "tsc -w",
"build": "tsc",
"version": "npm run changelog && git add CHANGELOG.md",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
"release": "conventional-github-releaser -p angular",
"deploy": "git push --follow-tags origin master && npm publish"
"deploy": "tsc && git push --follow-tags origin master && npm publish"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +28,7 @@
"yandex-dialogs-sdk",
"imperator"
],
"files": ["dist"],
"author": "Phil Romanov (@fletcherist)",
"license": "MIT",
"bugs": {
Expand All @@ -38,6 +41,7 @@
"ramda": "^0.25.0"
},
"devDependencies": {
"@types/node": "^10.5.1",
"conventional-changelog-cli": "^2.0.0",
"conventional-github-releaser": "^3.1.0",
"eslint": "^4.19.0",
Expand All @@ -46,6 +50,8 @@
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^3.0.1",
"jest": "^23.2.0"
"jest": "^23.2.0",
"typescript": "^2.9.2",
"typescript-eslint-parser": "^16.0.1"
}
}
4 changes: 2 additions & 2 deletions src/alice.js → src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Alice {
/* clear old sessions */
if (this.sessions.length > (this.config.sessionsLimit || 1000)) {
this.sessions.flush()
}
}

/* initializing session */
const sessionId = selectSessionId(req)
Expand All @@ -96,7 +96,7 @@ class Alice {
/* give control to the current scene */
if (session.getData('currentScene') !== null) {
const matchedScene = this.scenes.find(scene => {
return scene.name === session.getData('currentScene');
return scene.name === session.getData('currentScene')
})

/*
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/commands.js → src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Commands {
return figuresCommands.filter(figure => {
const reg = utils.getFiguresRegexp(figure.name)
return requestedCommandName.match(reg)
});
})
}

_searchRegexps(requestedCommandName) {
Expand Down Expand Up @@ -102,7 +102,7 @@ class Command {

if (typeof name === 'string') {
type = TYPE_STRING
if(name.includes('${')){
if (name.includes('${')) {
type = TYPE_FIGURE
}
} else if (name instanceof RegExp) {
Expand Down
File renamed without changes.
File renamed without changes.
19 changes: 0 additions & 19 deletions src/index.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import replyBuilder from './replyBuilder.js'

const Alice = require('./alice')
const Scene = require('./scene')

// fp
const button = require('./button')
const reply = require('./reply')

// for java-lovers
const buttonBuilder = require('./buttonBuilder')
// const replyBuilder = require('./replyBuilder')

export default Alice
export {
Alice,
Scene,
button,
reply,

buttonBuilder,
replyBuilder
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 13 additions & 12 deletions src/utils/index.js → src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function searchFiguresInString(template, string) {
return matched.filter(Boolean).slice(1)
}

function getFiguresRegexp(figure){
function getFiguresRegexp(figure) {
const searchTemplate = figure.replace(MATCH_REGEX, SEARCH_REGEX_STR)
return new RegExp(searchTemplate, 'ig')
}
Expand All @@ -32,25 +32,26 @@ function connectTokensWithFigures(tokens, figures) {
return res
}

function reversedInterpolation(template, string) {
function reversedInterpolation(template: string, string: string) {
if (!template) throw new Error('No template provided')
if (!string) throw new Error('No string provided')
const tokens = extractTemplateTokenNames(template)
const figures = searchFiguresInString(template, string)
return connectTokensWithFigures(tokens, figures)
}

module.exports.reversedInterpolation = reversedInterpolation

const selectCommand = req => req.request.command
const selectSession = req => req.session
const selectSessionId = req => selectSession(req).session_id
const selectUserId = req => selectSession(req).user_id
const isFunction = fn => fn && typeof fn === 'function'

module.exports.getFiguresRegexp = getFiguresRegexp
module.exports.selectCommand = selectCommand
module.exports.selectSession = selectSession
module.exports.selectSessionId = selectSessionId
module.exports.selectUserId = selectUserId
module.exports.isFunction = isFunction
const isFunction = (fn: Function) => fn && typeof fn === 'function'

export {
getFiguresRegexp,
selectCommand,
selectSession,
selectSessionId,
selectUserId,
isFunction,
reversedInterpolation
}
27 changes: 27 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": false,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
"types": ["node"],
// typeRoots option has been previously configured
"typeRoots": [
// add path to @types
"node_modules/@types"
]
},
"include": [
"src/**/*"
]
}

0 comments on commit 6dd23b1

Please sign in to comment.