Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

feat Support seam components#176

Merged
azat-co merged 32 commits into
mainfrom
support-seam-components
Apr 19, 2023
Merged

feat Support seam components#176
azat-co merged 32 commits into
mainfrom
support-seam-components

Conversation

@azat-co

@azat-co azat-co commented Apr 7, 2023

Copy link
Copy Markdown
Contributor

No description provided.

@azat-co azat-co requested a review from codetheweb as a code owner April 7, 2023 17:18
Comment thread src/types/models.ts Outdated

export type Event = Flatten<SeamEvent["event_type"]>

export interface ClientSessionI {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🐧 we don't generally add I/T suffixes in this repo

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's a good practice to distinguish interface from a type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we are going to introduce this, I think we should rename all interfaces to be consistent then.

@seveibar seveibar Apr 8, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • Most importantly: we should match existing repo convention to avoid bikeshedding (same rules as open source), so shouldn't be there

RE: general rule

  • should be prefix rather than suffix to match other Seam repos that use this convention
  • not sure there's a meaningful enough difference between the interface and a type, the convention makes the most sense where classes are used. Since we almost never use classes (this SDK is a bit of an exception), it's usually unnecessary. Would be strongly opposed to adding a T prefix to types and to a lesser extent the I prefix, since it's typically already distinguished by PascalCase and there are no classes so that's enough
  • If this comes up more often, having linter is better

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/seam-connect/client.ts Outdated
Authorization: `Bearer ${apiKey}`,
["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`,
Authorization: `Bearer ${apiKey || clientAccessToken}`,
// ["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// ["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`,
["User-Agent"]: `Javascript SDK v${version} (https://github.com/seamapi/javascript)`,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@codetheweb this line is causing browser errors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@codetheweb are you sure you want it back?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What are the errors? I'm assuming the browser doesn't like when JavaScript tries to override the user agent header. If so we probably need to selectively disable this in the browser environment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yeah we use the user agent in general to track SDK usage

maybe we could do a typeof window === "undefined" check?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Screenshot 2023-04-07 at 3 21 55 PM
the error is very annoying

Can we use another header name that's not violating some web standard like X-Seam-SDK-Version?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe we could

  1. keep user-agent for now until tooling is updated
  2. disable user-agent when in a browser
  3. always send a second header like x-seam-sdk-version with the same info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@codetheweb okay, so I left it for Node but removed for window for backwards compatibility
f55f201

with seam-sdk-version we have an error I'll open a new issue

Access to XMLHttpRequest at 'http://localhost:3020/devices/list' from origin 'http://localhost:5173' has been blocked by CORS policy: Request header field seam-sdk-version is not allowed by Access-Control-Allow-Headers in preflight response.

},
}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we generally throw instead of directly returning the error

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@codetheweb done ✅ df3898e

@azat-co

azat-co commented Apr 7, 2023

Copy link
Copy Markdown
Contributor Author

@seveibar any feedback would be appreciated.

Comment thread src/seam-connect/client.ts Outdated
}
}

type catParams = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PascalCase types, need linter here

Suggested change
type catParams = {
type CATParams = {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done ✅ a4124ff

Comment thread src/seam-connect/client.ts Outdated
params["user_identifier_key"] = ops.userIdentifierKey
try {
const response = await axios.post(
ops.endpoint + "internal/client_access_tokens/create",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
ops.endpoint + "internal/client_access_tokens/create",
ops.endpoint + "internal/client_access_tokens/get_or_create_empty",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it's more clear that it will reuse an existing client session this way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

we had this thread in the connect repo

@seveibar

seveibar commented Apr 8, 2023

Copy link
Copy Markdown
Contributor

I think the overall approach is good!

Comment thread package.json Outdated
"device"
],
"version": "5.12.0",
"version": "5.12.1",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How did this version change in this PR branch? This doesn't seems right.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@razor-x the PR bumps the version npm version patch

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should revert this. It will confuse semantic release. Versions are automatically released off of main when PRs are merged

@azat-co azat-co Apr 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@razor-x razor-x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Some questions on the option passing

Comment thread src/seam-connect/client.ts
Comment thread src/seam-connect/client.ts Outdated
/* Seam API Key */
apiKey?: string
/* Seam Client Access Token */
clientAccessToken?: string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I noticed in the new seam provider, we just pass all the keys as apiKey even if it's a clientAccessToken.

Do we still want a new option? Or just have apiKey, let the user pass whatever token they have, and then use the prefix if we need to do special logic?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think we should be generous in acceptance (since it's detectable) but we shouldn't conflate the options

@azat-co azat-co Apr 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread src/seam-connect/client.ts Outdated
}

type CSTParams = {
publishedKey?: string

@razor-x razor-x Apr 14, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs updates to match

Suggested change
publishedKey?: string
publishableKey?: string

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@razor-x

razor-x commented Apr 14, 2023

Copy link
Copy Markdown
Member

@azat-co To make it easier to grok so we can discuss, here are my proposed interface changes illustrated visually.

image

@seveibar

Copy link
Copy Markdown
Contributor

@razor-x disagree with making apiKey represent resources (sort of a polymorphic opaque type) if that's the proposal, simplifies implementation slightly at the risk of confusion

Comment thread src/types/models.ts Outdated
export type Event = Flatten<SeamEvent["event_type"]>

export interface ClientSession {
client_access_token_id: string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/client access token/client session token/g

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@seveibar yes, and also removed unnecessary properties a7730aa

Comment thread src/types/route-responses.ts Outdated
events: Event[]
}

export interface ClientSessionResponseInterface {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there a reason for the Interface suffix? Not our usual convention

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@seveibar changed ✅ ff9216f

Comment thread src/seam-connect/client.ts Outdated
Comment thread src/seam-connect/client.ts Outdated
/* Seam API Key */
apiKey?: string
/* Seam Client Access Token */
clientAccessToken?: string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think we should be generous in acceptance (since it's detectable) but we shouldn't conflate the options

@razor-x

razor-x commented Apr 14, 2023

Copy link
Copy Markdown
Member

i think we should be generous in acceptance (since it's detectable) but we shouldn't conflate the options

@seveibar

Yes so I think we should either avoid a new option and only have apiKey, or add a new option but not conflate them. So in that case if they try to provide both maybe that is an error, or if they pass in something with a mismatched prefix we should warn them.

@seveibar

Copy link
Copy Markdown
Contributor

Yes so I think we should either avoid a new option and only have apiKey, or add a new option but not conflate them. So in that case if they try to provide both maybe that is an error, or if they pass in something with a mismatched prefix we should warn them.

@razor-x yep agree, a warning/error that they need to use a different parameter is good. Could create an issue from this comment?

azat-co and others added 7 commits April 17, 2023 07:13
This reverts commit 5ab0dea.
…nflate the options

be generous in acceptance (since it's detectable) but we shouldn't conflate the options
Co-authored-by: Severin Ibarluzea <seveibar@gmail.com>
@azat-co

azat-co commented Apr 17, 2023

Copy link
Copy Markdown
Contributor Author

@seveibar

i think we should be generous in acceptance (since it's detectable) but we shouldn't conflate the options

done ✅ cfb2be2

a warning/error that they need to use a different parameter is good.

done ✅ 0e60d22

@razor-x

razor-x commented Apr 17, 2023

Copy link
Copy Markdown
Member

For reference, this is the branch with the current alpha version https://github.com/seamapi/javascript/tree/ver/5.15.0-alpha

Comment thread src/seam-connect/client.ts Outdated
clientAccessToken,
clientSessionToken,
} = getSeamClientOptionsWithDefaults(apiKeyOrOptions)
let bearer = `Bearer `

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This large nested if/else block is hard to parse. Perhaps easier to maintain if we move this to a function, then we can do early return. 🐧 E.g.,

const getBearerToken({ clientSessionToken, apiKey }) => {
   if (apiKey || clientAccessToken) {
     throw new Error(
        "You can't use clientSessionToken AND specify apiKey or clientAccessToken."
      )
    }
    if (clientSessionToken} {
      if (!clientSessionToken?.startsWith("seam_cst")) throw
      return clientSessionToken
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

agree that this function is a bit hard to parse and early return would work better (especially because this bearer var is being mutated)

Comment thread src/seam-connect/client.ts Outdated
const response: AxiosResponse & {
data: { client_session: ClientSession }
} = await axios.post(
(ops.endpoint ?? "https://connect.getseam.com") +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we re-use getSeamClientOptionsWithDefaults here? We are missing the fallback to SEAM_API_URL.

Comment thread src/seam-connect/client.ts Outdated
const response: AxiosResponse & {
data: { client_session: ClientSession }
} = await axios.post(
(ops.endpoint ?? "https://connect.getseam.com") +

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we normalize endpoint so we always strip any trailing slashes?

Comment thread src/seam-connect/client.ts
Comment thread src/seam-connect/client.ts Outdated
// backend mode
headers["seam-api-key"] = ops.apiKey
}
headers["seam-user-identifier-key"] = ops.userIdentifierKey

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
headers["seam-user-identifier-key"] = ops.userIdentifierKey
if (ops.userIdentifierKey) {
headers["seam-user-identifier-key"] = ops.userIdentifierKey
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Why?

@razor-x razor-x Apr 17, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can't remember exactly but without the conditional this was either generating a type error or a runtime error upstream when userIdentifierKey was null or undefined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done f5d6f95

Comment thread src/seam-connect/client.ts Outdated
/* Seam API Key */
apiKey?: string
/* Seam Client Access Token */
clientAccessToken?: string

@seveibar seveibar Apr 17, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clientAccessToken is mislabeled, seam_at should never be used on a client. Maybe personalAccessToken or just accessToken?

(the only thing that should be used on clients are seam_cst)

@azat-co azat-co Apr 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@seveibar It (AT) can be used on the backend which is the same code, right? We supported AT before this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is just the old name right? Should be clientSessionToken?

@seveibar seveibar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

agree w/ @razor-x 's comments, also would rename clientAccessToken to make sure nobody accidentally uses it on a client.

@azat-co

azat-co commented Apr 19, 2023

Copy link
Copy Markdown
Contributor Author

@seveibar @razor-x yes good catch
remove clientAccessToken ✅ 225366e
userIdentifierKey ✅ f5d6f95
getSeamClientOptionsWithDefaults ✅ 9aacc9b
getBearerToken ✅ 2b0b067

@razor-x razor-x left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, just noted the one issue with not passing though the axiosOptions.

throw new Error("userIdentifierKey is required")
}
try {
const response: AxiosResponse & {

@razor-x razor-x Apr 19, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@azat-co I didn't save my comment 😭

Anyway, was just going to say we should make sure to respect axiosOptions, so following the constructor, something like would work

    const client = axios.create({
      ...axiosOptions,
      baseURL: endpoint,
      headers,
    })

   await client.post(...)

Looking closer, I just realized the normal client behavior does a bit more, like always throw SeamAPIError. We should factor out the makeRequest method and use it both places. I will make an issue for that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@razor-x so do you mean instead of using axios directly, create a new instance with options? Makes sense!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, basically we should follow the same logic as the constructor for how we use axios. I created a new issue that better explains the desired behavior. It's ok if we resolve that issue in a follow-up PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@razor-x done ✅ 6da07a1

@azat-co azat-co force-pushed the support-seam-components branch from fd6ca02 to 6da07a1 Compare April 19, 2023 18:22
@azat-co azat-co merged commit 58d39bc into main Apr 19, 2023
@azat-co azat-co changed the title Support seam components feat Support seam components Apr 19, 2023
@seambot

seambot commented Apr 19, 2023

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 5.15.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants