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

feat(codegen): commit generated client types to codebase #3492

Merged
merged 11 commits into from
Mar 16, 2023
Merged
  •  
  •  
  •  
5 changes: 5 additions & 0 deletions .changeset/seven-keys-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/client-types": patch
---

feat(codegen): commit generated client types to codebase
40 changes: 40 additions & 0 deletions .github/workflows/codegen-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: OAS Codegen Build Check - BETA
on:
pull_request:
paths-ignore:
- "docs/**"
- "www/**"

jobs:
codegen-test:
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout
uses: actions/[email protected]
with:
fetch-depth: 0

- name: Setup Node.js environment
uses: actions/[email protected]
with:
node-version: "14"
cache: "yarn"

- name: Install dependencies
uses: ./.github/actions/cache-deps
with:
extension: codegen

- name: Build Packages - Force
run: yarn build --force --no-cache
Comment on lines +36 to +37
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Force build


- name: Assert latest codegen build committed
run: ./scripts/assert-codegen-build-committed-actions.sh
3 changes: 1 addition & 2 deletions packages/generated/client-types/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
/dist
/src/lib
/dist
121 changes: 121 additions & 0 deletions packages/generated/client-types/src/lib/core/ModelUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
/**
* Typing utilities from https:/sindresorhus/type-fest
*/

/**
* private methods for exportable dependencies
*/
// https:/sindresorhus/type-fest/blob/main/source/except.d.ts
type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true
? never
: KeyType extends ExcludeType
? never
: KeyType

// https:/sindresorhus/type-fest/blob/main/source/enforce-optional.d.ts
type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
? Type[Key] extends undefined
? Key
: never
: Key

type OptionalFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
? Type[Key] extends undefined
? never
: Key
: never

// https:/sindresorhus/type-fest/blob/main/source/merge.d.ts
type SimpleMerge<Destination, Source> = {
[Key in keyof Destination | keyof Source]: Key extends keyof Source
? Source[Key]
: Key extends keyof Destination
? Destination[Key]
: never
}

/**
* optional exportable dependencies
*/
export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {}

export type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <
G
>() => G extends B ? 1 : 2
? true
: false

export type Except<ObjectType, KeysType extends keyof ObjectType> = {
[KeyType in keyof ObjectType as Filter<
KeyType,
KeysType
>]: ObjectType[KeyType]
}

export type OmitIndexSignature<ObjectType> = {
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
? never
: KeyType]: ObjectType[KeyType]
}

export type PickIndexSignature<ObjectType> = {
[KeyType in keyof ObjectType as {} extends Record<KeyType, unknown>
? KeyType
: never]: ObjectType[KeyType]
}

export type EnforceOptional<ObjectType> = Simplify<
{
[Key in keyof ObjectType as RequiredFilter<
ObjectType,
Key
>]: ObjectType[Key]
} & {
[Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<
ObjectType[Key],
undefined
>
}
>

/**
* SetRequired
*/
export type SetRequired<BaseType, Keys extends keyof BaseType> = Simplify<
// Pick just the keys that are optional from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be required from the base type and make them required.
Required<Pick<BaseType, Keys>>
>

/**
* SetNonNullable
*/
export type SetNonNullable<
BaseType,
Keys extends keyof BaseType = keyof BaseType
> = {
[Key in keyof BaseType]: Key extends Keys
? NonNullable<BaseType[Key]>
: BaseType[Key]
}

/**
* Merge
*/
export type Merge<Destination, Source> = EnforceOptional<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>> &
SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
>

/**
* SetRelation
* Alias combining SetRequire and SetNonNullable.
*/
export type SetRelation<BaseType, Keys extends keyof BaseType> = SetRequired<
SetNonNullable<BaseType, Keys>,
Keys
>
5 changes: 5 additions & 0 deletions packages/generated/client-types/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */

export * from "./models"
Copy link
Member

Choose a reason for hiding this comment

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

question: Would it make sense in the end if we have multiple export from here, to namespace them like export * as Models so that when you import a type you now what it reference e.g Models.Address. It would avoid any collision as well if it happen that we attribute the same name in a different place. wdyt? This would be more of a convention like I am suggesting for medusa/types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One of the tenants for this initiative was to be a drop-in replacement of the current type imports from @medusajs/medusa in client code. I would not impose a namespace on end users. import * as Models can be used if need be.

Copy link
Member

Choose a reason for hiding this comment

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

If we export something else than the models later, the user will not be able to do as Models as it would also includes the rest. I agree that it is a drop in replacement so let’s not take this comment into account for now 👍

85 changes: 85 additions & 0 deletions packages/generated/client-types/src/lib/models/Address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { SetRelation, Merge } from "../core/ModelUtils"

import type { Country } from "./Country"
import type { Customer } from "./Customer"

/**
* An address.
*/
export interface Address {
/**
* ID of the address
*/
id: string
/**
* ID of the customer this address belongs to
*/
customer_id: string | null
/**
* Available if the relation `customer` is expanded.
*/
customer?: Customer | null
/**
* Company name
*/
company: string | null
/**
* First name
*/
first_name: string | null
/**
* Last name
*/
last_name: string | null
/**
* Address line 1
*/
address_1: string | null
/**
* Address line 2
*/
address_2: string | null
/**
* City
*/
city: string | null
/**
* The 2 character ISO code of the country in lower case
*/
country_code: string | null
/**
* A country object. Available if the relation `country` is expanded.
*/
country?: Country | null
/**
* Province
*/
province: string | null
/**
* Postal Code
*/
postal_code: string | null
/**
* Phone Number
*/
phone: string | null
/**
* The date with timezone at which the resource was created.
*/
created_at: string
/**
* The date with timezone at which the resource was updated.
*/
updated_at: string
/**
* The date with timezone at which the resource was deleted.
*/
deleted_at: string | null
/**
* An optional key-value map with additional details
*/
metadata: Record<string, any> | null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { SetRelation, Merge } from "../core/ModelUtils"

/**
* Address fields used when creating an address.
*/
export interface AddressCreatePayload {
/**
* First name
*/
first_name: string
/**
* Last name
*/
last_name: string
/**
* Phone Number
*/
phone?: string
company?: string
/**
* Address line 1
*/
address_1: string
/**
* Address line 2
*/
address_2?: string
/**
* City
*/
city: string
/**
* The 2 character ISO code of the country in lower case
*/
country_code: string
/**
* Province
*/
province?: string
/**
* Postal Code
*/
postal_code: string
/**
* An optional key-value map with additional details
*/
metadata?: Record<string, any>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { SetRelation, Merge } from "../core/ModelUtils"

/**
* Address fields used when creating/updating an address.
*/
export interface AddressPayload {
/**
* First name
*/
first_name?: string
/**
* Last name
*/
last_name?: string
/**
* Phone Number
*/
phone?: string
company?: string
/**
* Address line 1
*/
address_1?: string
/**
* Address line 2
*/
address_2?: string
/**
* City
*/
city?: string
/**
* The 2 character ISO code of the country in lower case
*/
country_code?: string
/**
* Province
*/
province?: string
/**
* Postal Code
*/
postal_code?: string
/**
* An optional key-value map with additional details
*/
metadata?: Record<string, any>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { SetRelation, Merge } from "../core/ModelUtils"

import type { OAuth } from "./OAuth"

export interface AdminAppsListRes {
apps: Array<OAuth>
}
10 changes: 10 additions & 0 deletions packages/generated/client-types/src/lib/models/AdminAppsRes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* istanbul ignore file */
/* tslint:disable */
/* eslint-disable */
import { SetRelation, Merge } from "../core/ModelUtils"

import type { OAuth } from "./OAuth"

export interface AdminAppsRes {
apps: OAuth
}
Loading