Skip to content

Commit ffa76bd

Browse files
committed
refactor: migrate to typescript, fixes #42
1 parent 2437eaf commit ffa76bd

36 files changed

+12580
-16478
lines changed

.babelrc

-9
This file was deleted.

.eslintrc.js

-11
This file was deleted.

package-lock.json

+9,614-13,120
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+17-41
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"version": "0.0.0-semantic-released",
44
"description": "JS SDK for Typeform API",
55
"scripts": {
6-
"test:unit": "MOCK_AXIOS=true jest ./tests/unit",
7-
"test:unit:watch": "MOCK_AXIOS=true jest ./tests/unit --watch",
8-
"test:integration": "jest ./tests/integration/",
9-
"test:integration:watch": "jest ./tests/integration/ --watch",
6+
"test:unit": "jest ./tests/unit",
7+
"test:unit:watch": "jest ./tests/unit --watch",
8+
"test:integration": "jest ./tests/integration",
9+
"test:integration:watch": "jest ./tests/integration --watch",
1010
"build": "rm -rf ./dist && rollup -c",
1111
"prepublish": "in-publish && npm run build || not-in-publish",
1212
"prepare": "npm run build",
13-
"lint": "standard ./src",
13+
"lint": "tslint --project tsconfig.json --config tslint.json",
1414
"semantic-release": "semantic-release",
1515
"server": "node ./tests/integration/mockServer.js",
1616
"server:dev": "nodemon ./tests/integration/mockServer.js"
@@ -51,61 +51,37 @@
5151
"axios": "^0.19.0"
5252
},
5353
"devDependencies": {
54-
"@babel/cli": "^7.0.0-rc.1",
55-
"@babel/core": "^7.0.0-rc.1",
56-
"@babel/node": "^7.0.0-rc.1",
57-
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-rc.1",
58-
"@babel/preset-env": "^7.0.0-rc.1",
5954
"@commitlint/cli": "^7.5.2",
6055
"@commitlint/config-conventional": "^7.5.0",
56+
"@types/jest": "^24.0.18",
6157
"axios-mock-adapter": "^1.17.0",
62-
"babel-core": "^7.0.0-bridge.0",
63-
"babel-eslint": "^8.2.3",
64-
"babel-jest": "^22.4.3",
65-
"babel-loader": "^7.1.4",
66-
"eslint": "^5.16.0",
67-
"eslint-config-standard": "^12.0.0",
68-
"eslint-plugin-import": "^2.17.2",
69-
"eslint-plugin-node": "^8.0.1",
70-
"eslint-plugin-promise": "^4.1.1",
71-
"eslint-plugin-standard": "^4.0.0",
7258
"husky": "^2.1.0",
7359
"in-publish": "^2.0.0",
74-
"jest": "^22.4.3",
60+
"jest": "^24.9.0",
7561
"json-server": "^0.14.0",
76-
"lint-staged": "^7.0.4",
62+
"lint-staged": "^9.2.5",
7763
"nodemon": "^1.18.3",
7864
"rollup": "^1.20.0",
7965
"rollup-plugin-commonjs": "^10.0.2",
66+
"rollup-plugin-copier": "^1.1.0",
8067
"rollup-plugin-json": "^4.0.0",
8168
"rollup-plugin-node-resolve": "^5.2.0",
8269
"rollup-plugin-terser": "^5.1.1",
70+
"rollup-plugin-typescript2": "^0.24.0",
8371
"semantic-release": "^15.13.19",
84-
"standard": "^11.0.1"
85-
},
86-
"lint-staged": {
87-
"*.js": [
88-
"standard",
89-
"git add"
90-
]
72+
"ts-jest": "^24.0.2",
73+
"tslint": "^5.19.0",
74+
"tslint-config-standard": "^8.0.1",
75+
"typescript": "^3.6.2"
9176
},
9277
"jest": {
9378
"automock": false,
94-
"setupFiles": [
95-
"./tests/setupJest.js"
96-
],
97-
"testURL": "http://localhost/"
98-
},
99-
"standard": {
100-
"env": [
101-
"jest"
102-
],
103-
"globals": [
104-
"axios"
105-
]
79+
"testURL": "http://localhost/",
80+
"preset": "ts-jest"
10681
},
10782
"husky": {
10883
"hooks": {
84+
"pre-commit": "npm run lint",
10985
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
11086
}
11187
}

rollup.config.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import commonjs from 'rollup-plugin-commonjs'
2+
import copier from 'rollup-plugin-copier'
23
import json from 'rollup-plugin-json'
34
import resolveModule from 'rollup-plugin-node-resolve'
5+
import typescript from 'rollup-plugin-typescript2'
46
import pkg from './package.json'
57
import { terser } from 'rollup-plugin-terser'
68

9+
const copy = copier({
10+
items: [{
11+
src: 'src/typeform-types.ts',
12+
dest: 'dist/typeform-types.ts',
13+
createPath: true
14+
}]
15+
})
16+
717
const onwarn = (warning, rollupWarn) => {
818
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
919
rollupWarn(warning)
@@ -14,11 +24,13 @@ const plugins = [
1424
commonjs({
1525
include: ['node_modules/**']
1626
}),
17-
json()
27+
typescript(),
28+
json(),
29+
copy
1830
]
1931

2032
export default [{
21-
input: 'src/index.js',
33+
input: 'src/index.ts',
2234
output: [{
2335
file: pkg.main,
2436
format: 'cjs',
@@ -36,14 +48,17 @@ export default [{
3648
}),
3749
...plugins
3850
],
39-
external: ['http', 'https', 'url', 'zlib', 'assert', 'stream', 'tty', 'util', 'os']
51+
external: ['http', 'https', 'url', 'zlib', 'assert', 'stream', 'tty', 'util', 'os', 'tslib']
4052
}, {
41-
input: 'src/index.js',
53+
input: 'src/index.ts',
4254
output: {
4355
file: pkg.browser,
4456
format: 'umd',
4557
name: 'typeformAPI',
46-
exports: 'named'
58+
exports: 'named',
59+
globals: {
60+
tslib: 'tslib'
61+
}
4762
},
4863
onwarn,
4964
plugins: [
@@ -52,5 +67,6 @@ export default [{
5267
}),
5368
terser(),
5469
...plugins
55-
]
70+
],
71+
external: ['tslib']
5672
}]

src/constants/index.js renamed to src/constants/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const FONTS_AVAILABLE = [
1+
import { Typeform } from '../typeform-types'
2+
3+
export const FONTS_AVAILABLE: Typeform.Font[] = [
24
'Acme',
35
'Arial',
46
'Arvo',
@@ -33,4 +35,4 @@ export const FONTS_AVAILABLE = [
3335
'Vollkorn'
3436
]
3537

36-
export const API_BASE_URL = 'https://api.typeform.com'
38+
export const API_BASE_URL: Typeform.API_URL = 'https://api.typeform.com'

src/create-client.js renamed to src/create-client.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import axios from 'axios'
22
import { API_BASE_URL } from './constants'
3+
import { Typeform } from './typeform-types'
34

4-
export const clientConstructor = ({ token, ...options }) => {
5+
export const clientConstructor = ({ token, ...options }: Typeform.ClientArg): Typeform.HTTPClient => {
56
return {
6-
request: (args) => {
7+
request: (args: Typeform.Request) => {
78
const {
89
url,
910
data,
@@ -23,6 +24,7 @@ export const clientConstructor = ({ token, ...options }) => {
2324
...otherArgs
2425
}
2526

27+
// @ts-ignore
2628
return axios({
2729
url: requestUrl,
2830
...requestParameters,
@@ -34,8 +36,8 @@ export const clientConstructor = ({ token, ...options }) => {
3436
Authorization: `bearer ${token}`
3537
}
3638
})
37-
.then(response => response.data)
38-
.catch(error => {
39+
.then((response): any => response.data)
40+
.catch((error: any) => {
3941
if (error && error.response && error.response.data && error.response.data.description) {
4042
throw new Error(error.response.data.description)
4143
} else {
@@ -46,7 +48,7 @@ export const clientConstructor = ({ token, ...options }) => {
4648
}
4749
}
4850

49-
export const buildUrlWithParams = (url, params = {}) => {
51+
export const buildUrlWithParams = (url: string, params: Typeform.DocumentData = {}): string => {
5052
const queryParams = Object.keys(params)
5153
.filter((key) => params[key] !== undefined && params[key] !== null)
5254
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)

src/forms.js

-76
This file was deleted.

0 commit comments

Comments
 (0)