Skip to content

Commit

Permalink
fix: encodeParam returned [object Object] for object types (#1047)
Browse files Browse the repository at this point in the history
`@looker/sdk-rtl` was encoding object values like

```ts
{ created_date: 'this year to second' }
```

as 
```ts
[object Object]
```

and now the value is JSON stringified instead
  • Loading branch information
jkaster authored Apr 7, 2022
1 parent 5a183be commit eccc1db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/sdk-rtl/src/transport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import { TestConfig } from './testUtils'
import { encodeParam, ResponseMode, responseMode } from './transport'
import { DelimArray } from './delimArray'

const config = TestConfig()
const binaryTypes = config.testData.content_types.binary as [string]
Expand Down Expand Up @@ -56,10 +57,15 @@ describe('Transport', () => {

it('encodeParam', () => {
const today = new Date('01 January 2020 14:48 UTC')
const ra = new DelimArray([1, 2, 3])
expect(encodeParam(ra)).toEqual('1%2C2%2C3')
expect(encodeParam(today)).toEqual('2020-01-01T14%3A48%3A00.000Z')
expect(encodeParam('foo%2Fbar')).toEqual('foo%2Fbar')
expect(encodeParam('foo/bar')).toEqual('foo%2Fbar')
expect(encodeParam(true)).toEqual('true')
expect(encodeParam(2.3)).toEqual('2.3')
expect(encodeParam({ created_date: 'this year to second' })).toEqual(
'{"created_date":"this year to second"}'
)
})
})
7 changes: 6 additions & 1 deletion packages/sdk-rtl/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type { Agent } from 'https'
import type { Headers } from 'request'
import type { Readable } from 'readable-stream'
import { matchCharsetUtf8, matchModeBinary, matchModeString } from './constants'
import { DelimArray } from './delimArray'

export const agentPrefix = 'TS-SDK'
export const LookerAppId = 'x-looker-appid'
Expand Down Expand Up @@ -379,8 +380,12 @@ export type Values = { [key: string]: any } | null | undefined
export function encodeParam(value: any) {
if (value instanceof Date) {
value = value.toISOString()
} else if (value instanceof DelimArray) {
value = value.toString()
}
let encoded = value.toString()
// check for object type to prevent "[object Object]" as the value.toString()
let encoded =
typeof value === 'object' ? JSON.stringify(value) : value.toString()

// decodeURIComponent throws URIError if there is a % character
// without it being part of an encoded
Expand Down

0 comments on commit eccc1db

Please sign in to comment.