Skip to content

Commit

Permalink
improve generated resource types, remove tests type casts (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kainpets authored Jul 9, 2024
1 parent 2d8d040 commit 8e8dd5d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
22 changes: 19 additions & 3 deletions src/lib/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type Property =
| RecordProperty
| ListProperty
| ObjectProperty
| BooleanProperty

interface StringProperty extends BaseProperty {
type: 'string'
Expand All @@ -113,6 +114,9 @@ interface RecordProperty extends BaseProperty {
interface ListProperty extends BaseProperty {
type: 'list'
}
interface BooleanProperty extends BaseProperty {
type: 'boolean'
}

interface ObjectProperty extends BaseProperty {
type: 'object'
Expand Down Expand Up @@ -260,9 +264,12 @@ const createResources = (
typeof schema.properties === 'object' &&
schema.properties !== null
) {
acc[schemaName] = {
resourceType: schemaName,
properties: createProperties(schema.properties),
return {
...acc,
[schemaName]: {
resourceType: schemaName,
properties: createProperties(schema.properties),
},
}
}
return acc
Expand Down Expand Up @@ -405,6 +412,13 @@ const createProperties = (
if ('type' in prop) {
switch (prop.type) {
case 'string':
if ('enum' in prop && Array.isArray(prop.enum)) {
return {
...baseProperty,
type: 'enum',
values: prop.enum.map((value) => ({ name: String(value) })),
}
}
return { ...baseProperty, type: 'string' }
case 'object':
return {
Expand All @@ -419,6 +433,8 @@ const createProperties = (
}
case 'array':
return { ...baseProperty, type: 'list' }
case 'boolean':
return { ...baseProperty, type: 'boolean' }
default:
return { ...baseProperty, type: 'string' }
}
Expand Down
2 changes: 1 addition & 1 deletion test/blueprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createBlueprint } from '@seamapi/blueprint'
import * as types from './fixtures/types/index.js'

test('createBlueprint', (t) => {
// @ts-expect-error Remove once the fixture is propely typed
// @ts-expect-error Remove once the fixture is properly typed
const blueprint = createBlueprint(types)
t.snapshot(blueprint, 'blueprint')
})
5 changes: 2 additions & 3 deletions test/seam-blueprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import test from 'ava'

import { createBlueprint } from '@seamapi/blueprint'

import type { Openapi } from 'lib/openapi.js'

test('createBlueprint', (t) => {
const blueprint = createBlueprint({ openapi: openapi as unknown as Openapi })
// @ts-expect-error Remove once the import is properly typed
const blueprint = createBlueprint({ openapi })
t.snapshot(blueprint, 'blueprint')
})
64 changes: 58 additions & 6 deletions test/snapshots/seam-blueprint.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ Generated by [AVA](https://avajs.dev).
description: '',
isDeprecated: false,
name: 'can_add_acs_users_to_acs_access_groups',
type: 'string',
type: 'boolean',
},
{
deprecationMessage: '',
description: '',
isDeprecated: false,
name: 'can_automate_enrollment',
type: 'string',
type: 'boolean',
},
{
deprecationMessage: '',
description: '',
isDeprecated: false,
name: 'can_create_acs_access_groups',
type: 'string',
type: 'boolean',
},
{
deprecationMessage: '',
description: '',
isDeprecated: false,
name: 'can_remove_acs_users_from_acs_access_groups',
type: 'string',
type: 'boolean',
},
{
deprecationMessage: '',
Expand Down Expand Up @@ -74,7 +74,33 @@ Generated by [AVA](https://avajs.dev).
description: '',
isDeprecated: false,
name: 'external_type',
type: 'string',
type: 'enum',
values: [
{
name: 'pti_site',
},
{
name: 'alta_org',
},
{
name: 'salto_site',
},
{
name: 'brivo_account',
},
{
name: 'hid_credential_manager_organization',
},
{
name: 'visionline_system',
},
{
name: 'assa_abloy_credential_service',
},
{
name: 'latch_building',
},
],
},
{
deprecationMessage: '',
Expand Down Expand Up @@ -113,7 +139,33 @@ Generated by [AVA](https://avajs.dev).
`,
isDeprecated: false,
name: 'system_type',
type: 'string',
type: 'enum',
values: [
{
name: 'pti_site',
},
{
name: 'alta_org',
},
{
name: 'salto_site',
},
{
name: 'brivo_account',
},
{
name: 'hid_credential_manager_organization',
},
{
name: 'visionline_system',
},
{
name: 'assa_abloy_credential_service',
},
{
name: 'latch_building',
},
],
},
{
deprecationMessage: '',
Expand Down
Binary file modified test/snapshots/seam-blueprint.test.ts.snap
Binary file not shown.

0 comments on commit 8e8dd5d

Please sign in to comment.