Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encodeParam returned [object Object] for object types #1047

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks so familiar. I thought we'd fixed it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at least we've fixed it now

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