Skip to content

Commit adccc13

Browse files
authored
feat(PR-40): update theme methods (#104)
Update `Typeform.Theme` type to include new types from docs: https://www.typeform.com/developers/create/reference/create-theme/ Fix `theme.create` and `theme.update` methods to send payload to API. Update the bin to load payload from a file: `params.json` as JSON or `params.js` as JavaScript object. It is easier to pass large payloads like this compared to passing it via command line.
1 parent e32b363 commit adccc13

File tree

7 files changed

+83
-12
lines changed

7 files changed

+83
-12
lines changed

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# output folder
22
dist/
33
rollup.config.js
4+
params.js
5+
params.json

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ lib
44
.npmrc
55
yarn-error.log
66
.DS_STORE
7-
.idea/
7+
.idea/
8+
params.js
9+
params.json

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ Each one of them encapsulates the operations related to it (like listing, updati
227227
- Creates a theme with the given configuration
228228
- See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/create-theme/)
229229

230-
#### `themes.update({ background, colors, font, hasTransparentButton, name })`
230+
#### `themes.update({ id, background, colors, font, hasTransparentButton, name })`
231231

232-
- Updates a theme with the given configuration
232+
- Updates a theme with the given configuration, requires `id`
233233
- See more details of the payload in [the documentation](https://developer.typeform.com/create/reference/update-theme/)
234234

235235
#### `themes.delete({ id })`

src/bin.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { inspect } from 'util'
2+
import { readFileSync, existsSync } from 'fs'
23

34
import { createClient, Typeform } from './index'
45

@@ -34,13 +35,25 @@ if (!typeformAPI[property]?.[method]) {
3435
}
3536

3637
let parsedParams = undefined
38+
let normalizedParams = undefined
3739

3840
if (methodParams && methodParams.length > 0) {
3941
const methodParamsString = methodParams.join(',')
40-
const normalizedParams = methodParamsString.startsWith('{')
42+
normalizedParams = methodParamsString.startsWith('{')
4143
? methodParamsString
4244
: `{${methodParamsString}}`
45+
} else {
46+
const dir = process.cwd()
47+
const jsonFile = `${dir}/params.json`
48+
const jsFile = `${dir}/params.js`
49+
if (existsSync(jsonFile)) {
50+
normalizedParams = `JSON.parse(\`${readFileSync(jsonFile, 'utf-8')}\`)`
51+
} else if (existsSync(jsFile)) {
52+
normalizedParams = readFileSync(jsFile, 'utf-8')
53+
}
54+
}
4355

56+
if (normalizedParams) {
4457
try {
4558
// this eval executes code supplied by user on their own machine, this is safe
4659
// eslint-disable-next-line no-eval

src/themes.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ export class Themes {
66
constructor(private _http: Typeform.HTTPClient) {}
77

88
public create(args: {
9-
id?: string
109
background?: Typeform.ThemeBackground
1110
colors: Typeform.ThemeColors
1211
font: Typeform.Font
1312
hasTransparentButton?: boolean
1413
name: string
14+
fields?: Typeform.ThemeFontSizeAndAlignment
15+
screens?: Typeform.ThemeFontSizeAndAlignment
16+
roundedCorners?: Typeform.ThemeRoundedCorners
1517
}): Promise<Typeform.Theme> {
1618
return createOrUpdateTheme(this._http, args)
1719
}
@@ -58,13 +60,19 @@ export class Themes {
5860
}
5961

6062
public update(args: {
61-
id?: string
63+
id: string
6264
background?: Typeform.ThemeBackground
6365
colors: Typeform.ThemeColors
6466
font: Typeform.Font
6567
hasTransparentButton?: boolean
6668
name: string
69+
fields?: Typeform.ThemeFontSizeAndAlignment
70+
screens?: Typeform.ThemeFontSizeAndAlignment
71+
roundedCorners?: Typeform.ThemeRoundedCorners
6772
}): Promise<Typeform.Theme> {
73+
if (!args.id) {
74+
throw new Error(`The property id is required`)
75+
}
6876
return createOrUpdateTheme(this._http, args)
6977
}
7078
}
@@ -78,9 +86,22 @@ const createOrUpdateTheme = (
7886
font: Typeform.Font
7987
hasTransparentButton?: boolean
8088
name: string
89+
fields?: Typeform.ThemeFontSizeAndAlignment
90+
screens?: Typeform.ThemeFontSizeAndAlignment
91+
roundedCorners?: Typeform.ThemeRoundedCorners
8192
}
8293
): Promise<Typeform.Theme> => {
83-
const { id, background, colors, font, hasTransparentButton, name } = args
94+
const {
95+
id,
96+
background,
97+
colors,
98+
font,
99+
hasTransparentButton,
100+
name,
101+
fields,
102+
screens,
103+
roundedCorners,
104+
} = args
84105
// check if required properties are defined
85106
const requiredProperties: Typeform.Theme = { name, font, colors }
86107
Object.getOwnPropertyNames(requiredProperties).forEach(
@@ -98,10 +119,15 @@ const createOrUpdateTheme = (
98119
return http.request({
99120
method: id ? 'put' : 'post',
100121
url: id ? `/themes/${id}` : '/themes',
101-
background,
102-
colors,
103-
font,
104-
has_transparent_button: !!hasTransparentButton,
105-
name,
122+
data: {
123+
background,
124+
colors,
125+
font,
126+
has_transparent_button: !!hasTransparentButton,
127+
fields,
128+
screens,
129+
rounded_corners: roundedCorners,
130+
name,
131+
},
106132
})
107133
}

src/typeform-types.ts

+23
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,10 @@ export namespace Typeform {
11401140
* Colors the theme will apply to answers, background, buttons, and questions.
11411141
*/
11421142
colors?: ThemeColors
1143+
/**
1144+
* Font size and alignment for fields.
1145+
*/
1146+
fields?: ThemeFontSizeAndAlignment
11431147
/**
11441148
* Default: `"Source Sans Pro"`
11451149
* Font for the theme.
@@ -1157,6 +1161,14 @@ export namespace Typeform {
11571161
* Name of the theme.
11581162
*/
11591163
name?: string
1164+
/**
1165+
* Specifies border radius style of buttons and other elements in the form.
1166+
*/
1167+
rounded_corners?: ThemeRoundedCorners
1168+
/**
1169+
* Font size and alignment for welcome and thankyou screens.
1170+
*/
1171+
screens?: ThemeFontSizeAndAlignment
11601172
/**
11611173
* Default: `"private"`
11621174
* Specifies whether the theme is `public` (one of Typeform's built-in themes that are available in all accounts) or `private`
@@ -1203,6 +1215,17 @@ export namespace Typeform {
12031215
*/
12041216
question?: string
12051217
}
1218+
/**
1219+
* Font size and alignment.
1220+
*/
1221+
export interface ThemeFontSizeAndAlignment {
1222+
alignment?: 'left' | 'center'
1223+
font_size?: 'small' | 'medium' | 'large'
1224+
}
1225+
/**
1226+
* Specifies border radius style of buttons and other elements in the form.
1227+
*/
1228+
export type ThemeRoundedCorners = 'none' | 'small' | 'large'
12061229
/**
12071230
* Object that specifies the settings and properties for the form's thank you screen.
12081231
*/

tests/unit/themes.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,8 @@ test('Updating a theme has the correct path and method', async () => {
9999
expect(axios.history.put[0].url).toBe(`${API_BASE_URL}/themes/2`)
100100
expect(axios.history.put[0].method).toBe('put')
101101
})
102+
103+
test('Updating a theme without id throws', async () => {
104+
// @ts-ignore
105+
expect(() => themesRequest.update(mockThemePayload)).toThrow()
106+
})

0 commit comments

Comments
 (0)