superfaceai / one-sdk-js

1️⃣ One Node.js SDK for all the APIs you want to integrate with
https://superface.ai
MIT License
46 stars 3 forks source link

Shared: Introduce `None` type #314

Closed freaz closed 1 year ago

freaz commented 1 year ago

Description

Adds None as possible Variables value.

Motivation and Context

Incorrect handling of null and undefined lead to confusion by end users, and we internally introduced bugs as TypeScript types didn't allow null as a possible value.

Updated specification: https://github.com/superfaceai/spec/pull/47

Decision Record internal

Context

When we were implementing Binary Data, we discovered a major flaw in types and how they interact with required/optional fields and nullable values.

We now have only JavaScript environment.

Decision

None is defined as union of undefined and null.

It will be added as possible Variables value.

None CANNOT be used as primitive type, it is implicit type for nullable values.

We will add NoneLiteral to support undefined and null values in Examples.

By default, fields are Optional.

Required field MUST fail validation only if the field in the object isn’t defined at all. So, it doesn’t matter whether the value is None or any other Model. Behaviour similar to Object.hasOwn() in JavaScript.

By default, value is Optional.

Required value CANNOT be None.

Example

usecase Example {
  input {
    field1 string // optional value can be String or None, in JS: 'value', undefined, null
    field2 string! // required value can be String, in JS: 'value'
  }

  example Success {
    input {
      field1 = None
            field2 = 'value'
    }
  }
}

If we surfaced types for profile’s input, field1 would be typed as Optional<string>, which can be represented also as string | None and fully expanded as string | null | undefined.

For field2, type is simply string.

Consequences

Clearly defined behaviour of absence of the value for user, as well internal handling of undefined and null values.

Types of changes

Checklist:

freaz commented 1 year ago

Rebased on top of dev