Skip to content

Commit 7dd8477

Browse files
refactor: migrate to typescript, fixes #42
2 parents 925dccd + 0634547 commit 7dd8477

36 files changed

+9056
-11160
lines changed

.babelrc

-9
This file was deleted.

.eslintrc.js

-11
This file was deleted.

package-lock.json

+5,167-7,625
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+28-49
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
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"
1717
},
1818
"main": "dist/index.cjs.js",
1919
"browser": "dist/typeform-api.js",
2020
"module": "dist/index.esm.js",
21+
"typings": "dist/index.d.ts",
2122
"engines": {
2223
"node": ">=8"
2324
},
@@ -51,61 +52,39 @@
5152
"axios": "^0.19.0"
5253
},
5354
"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",
59-
"@commitlint/cli": "^7.5.2",
60-
"@commitlint/config-conventional": "^7.5.0",
55+
"@commitlint/cli": "^8.1.0",
56+
"@commitlint/config-conventional": "^8.1.0",
57+
"@types/jest": "^24.0.18",
6158
"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",
72-
"husky": "^2.1.0",
59+
"husky": "^4.0.0-beta.1",
7360
"in-publish": "^2.0.0",
74-
"jest": "^22.4.3",
75-
"json-server": "^0.14.0",
76-
"lint-staged": "^7.0.4",
77-
"nodemon": "^1.18.3",
78-
"rollup": "^1.20.0",
79-
"rollup-plugin-commonjs": "^10.0.2",
61+
"jest": "^24.9.0",
62+
"json-server": "^0.15.1",
63+
"lint-staged": "^9.2.5",
64+
"nodemon": "^1.19.2",
65+
"rollup": "^1.21.2",
66+
"rollup-plugin-commonjs": "^10.1.0",
67+
"rollup-plugin-copier": "^1.1.0",
8068
"rollup-plugin-json": "^4.0.0",
69+
"rollup-plugin-node-builtins": "^2.1.2",
70+
"rollup-plugin-node-globals": "^1.4.0",
8171
"rollup-plugin-node-resolve": "^5.2.0",
8272
"rollup-plugin-terser": "^5.1.1",
83-
"semantic-release": "^15.13.19",
84-
"standard": "^11.0.1"
85-
},
86-
"lint-staged": {
87-
"*.js": [
88-
"standard",
89-
"git add"
90-
]
73+
"rollup-plugin-typescript2": "^0.24.1",
74+
"semantic-release": "^15.13.24",
75+
"ts-jest": "^24.0.2",
76+
"tslint": "^5.20.0",
77+
"tslint-config-standard": "^8.0.1",
78+
"typescript": "^3.6.2"
9179
},
9280
"jest": {
9381
"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-
]
82+
"testURL": "http://localhost/",
83+
"preset": "ts-jest"
10684
},
10785
"husky": {
10886
"hooks": {
87+
"pre-commit": "npm run lint",
10988
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
11089
}
11190
}

rollup.config.js

+34-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
import builtins from 'rollup-plugin-node-builtins'
12
import commonjs from 'rollup-plugin-commonjs'
3+
import copier from 'rollup-plugin-copier'
24
import json from 'rollup-plugin-json'
5+
import globals from 'rollup-plugin-node-globals'
36
import resolveModule from 'rollup-plugin-node-resolve'
4-
import pkg from './package.json'
57
import { terser } from 'rollup-plugin-terser'
8+
import typescript from 'rollup-plugin-typescript2'
9+
import pkg from './package.json'
10+
11+
const copy = copier({
12+
items: [{
13+
src: 'src/typeform-types.ts',
14+
dest: 'dist/typeform-types.ts',
15+
createPath: true
16+
}]
17+
})
618

719
const onwarn = (warning, rollupWarn) => {
820
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
@@ -11,14 +23,13 @@ const onwarn = (warning, rollupWarn) => {
1123
}
1224

1325
const plugins = [
14-
commonjs({
15-
include: ['node_modules/**']
16-
}),
17-
json()
26+
typescript(),
27+
json(),
28+
copy
1829
]
1930

2031
export default [{
21-
input: 'src/index.js',
32+
input: 'src/index.ts',
2233
output: [{
2334
file: pkg.main,
2435
format: 'cjs',
@@ -30,27 +41,36 @@ export default [{
3041
}],
3142
onwarn,
3243
plugins: [
33-
resolveModule({
34-
mainFields: ['main', 'module'],
35-
preferBuiltins: true
44+
resolveModule(),
45+
commonjs({
46+
include: ['node_modules/**']
3647
}),
3748
...plugins
3849
],
3950
external: ['http', 'https', 'url', 'zlib', 'assert', 'stream', 'tty', 'util', 'os']
4051
}, {
41-
input: 'src/index.js',
52+
input: 'src/index.ts',
4253
output: {
4354
file: pkg.browser,
4455
format: 'umd',
4556
name: 'typeformAPI',
46-
exports: 'named'
57+
exports: 'named',
58+
intro: 'const global = window;'
4759
},
4860
onwarn,
4961
plugins: [
5062
resolveModule({
51-
mainFields: ['browser']
63+
browser: true
5264
}),
53-
terser(),
54-
...plugins
65+
commonjs({
66+
include: ['node_modules/**'],
67+
browser: true,
68+
preferBuiltins: false,
69+
ignoreGlobal: false
70+
}),
71+
...plugins,
72+
globals(),
73+
builtins(),
74+
terser()
5575
]
5676
}]

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)