PaddleHQ / paddle-js-wrapper

Wrapper to load Paddle.js as a module and use TypeScript definitions when working with methods.
Apache License 2.0
44 stars 6 forks source link

fix(Types): update customData type #61

Closed danbillson closed 2 months ago

danbillson commented 2 months ago

Changes:

Updates the customData type to match as is in shared.ts

In paddle-js-v2 we actually allow any valid object including nested objects so this updates the type to reflect that

  if (hasValue(input.customData)) {
    try {
      // When we pass this value using `data-custom-data` html attribute it will be a string. So we are parsing it to check its validity
      const customData = typeof input.customData === 'string' ? JSON.parse(input.customData) : input.customData
      if (isObjectValid(customData)) {
        checkoutProps.customData = JSON.stringify(customData)
      } else {
        throw new Error('Invalid custom data')
      }
    } catch (e) {
      logger.log(INVALID_CUSTOM_DATA, LOG_LEVEL.WARNING, true)
    }
  }

Testing

Tested locally with pnpm link

    paddle?.Checkout.open({
      items: [{ priceId: id, quantity: 1 }],
      customData: {
        foo: {
          bar: "baz",
        }
      }
    });

image

is now a valid option, but things like:

customData: []
or
customData: new Map([["foo", "bar"]]),
or
customData: null

remain invalid