Skip to content

Commit ba533b1

Browse files
feat(create-client): remove need for token
2 parents 7dd8477 + 2a522a2 commit ba533b1

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ npm install @typeform/api-client --save
5050
2. Create an instance with your personal token
5151

5252
``` javascript
53-
const typeformAPI = createClient({
54-
token: '<your token>'
55-
})
53+
const typeformAPI = createClient({ token: '<your token>' })
5654
```
5755

5856
3. Use any of the methods available in the [reference](#reference)
@@ -75,9 +73,10 @@ npm install @typeform/api-client --save
7573
- Returns an instance with the methods described below
7674

7775
``` javascript
78-
const typeformClient = createClient({
79-
token: '<your token>'
80-
})
76+
const typeformClient = createClient({ token: '<your token>' })
77+
78+
// If what you are trying to acces doesn't require a token, you can construct the client without any argument
79+
const typeformAPI = createClient()
8180
```
8281

8382
Client returns the following properties:

src/create-client.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import axios from 'axios'
22
import { API_BASE_URL } from './constants'
33
import { Typeform } from './typeform-types'
44

5-
export const clientConstructor = ({ token, ...options }: Typeform.ClientArg): Typeform.HTTPClient => {
5+
export const clientConstructor = ({ token, ...options }: Typeform.ClientArg = {}): Typeform.HTTPClient => {
66
return {
77
request: (args: Typeform.Request) => {
88
const {
@@ -15,14 +15,9 @@ export const clientConstructor = ({ token, ...options }: Typeform.ClientArg): Ty
1515

1616
const requestUrl = buildUrlWithParams(`${API_BASE_URL}${url}`, params)
1717

18-
const {
19-
headers = {}
20-
} = options
21-
22-
const requestParameters = {
23-
...options,
24-
...otherArgs
25-
}
18+
const { headers = {} } = options
19+
const authorization = token ? { Authorization: `bearer ${token}` } : {}
20+
const requestParameters = { ...options, ...otherArgs }
2621

2722
// @ts-ignore
2823
return axios({
@@ -33,7 +28,7 @@ export const clientConstructor = ({ token, ...options }: Typeform.ClientArg): Ty
3328
Accept: 'application/json, text/plain, */*',
3429
...headers,
3530
...argsHeaders,
36-
Authorization: `bearer ${token}`
31+
...authorization
3732
}
3833
})
3934
.then((response: any) => response.data)

src/index.ts

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import { Typeform } from './typeform-types'
1111
export { Typeform } from './typeform-types'
1212

1313
export const createClient = (args: Typeform.ClientArg = { token: null }) => {
14-
if (!args.token) {
15-
throw new Error('Token is missing')
16-
}
17-
1814
const http = clientConstructor(args)
1915

2016
return {

src/typeform-types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export namespace Typeform {
156156
* Argument object for Typeform API client
157157
*/
158158
export interface ClientArg extends DocumentData {
159-
token: string
159+
token?: string
160160
}
161161
/**
162162
* Conditions for executing the Logic Jump. Conditions answer the question, "Under what circumstances?"

tests/unit/index.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ test('client constructor has a request function property', () => {
1414
expect(client.request).toBeDefined()
1515
})
1616

17-
test('Initialising fails when missing the token', () => {
18-
expect(() => createClient()).toThrow('Token is missing')
17+
test('Initialising does not fail when missing the token', () => {
18+
expect(() => createClient()).not.toThrow()
1919
})

0 commit comments

Comments
 (0)