Skip to content

Commit

Permalink
Added type for when references are only returned as id and type
Browse files Browse the repository at this point in the history
  • Loading branch information
sroebert committed Sep 12, 2024
1 parent f9b8e16 commit a212798
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/lib/stack/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const defaultInterfaces = (prefix = '', systemFields = false) => {
time: string;
user: string;
}`,
`export interface ${prefix}File {
`export interface ${prefix}File {
uid: string;
created_at: string;
updated_at: string;
Expand All @@ -25,11 +25,15 @@ export const defaultInterfaces = (prefix = '', systemFields = false) => {
title: string;
publish_details: ${prefix}PublishDetails;
}`,
`export interface ${prefix}Link {
`export interface ${prefix}Link {
title: string;
href: string;
}`,
`export interface ${prefix}Taxonomy {
`export interface ${prefix}Reference {
uid: string;
_content_type_uid: string;
}`,
`export interface ${prefix}Taxonomy {
taxonomy_uid: string;
max_terms?: number;
mandatory: boolean;
Expand Down
6 changes: 5 additions & 1 deletion src/lib/tsgen/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,12 @@ export default function (userOptions: TSGenOptions) {
return name
}

function type_reference_id() {
return `${options.naming?.prefix}Reference`
}

function type_reference(field: ContentstackTypes.Field) {
const references: string[] = []
const references: string[] = [type_reference_id()]

if (Array.isArray(field.reference_to)) {
field.reference_to.forEach(v => {
Expand Down
4 changes: 2 additions & 2 deletions tests/tsgen/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ describe("references", () => {
_version?: 5 ;
title: string ;
url: string ;
single_reference: (IReferenceChild)[] ;
multiple_reference?: (IReferenceChild | IBoolean | IBuiltinExample)[] ;
single_reference: (IReference | IReferenceChild)[] ;
multiple_reference?: (IReference | IReferenceChild | IBoolean | IBuiltinExample)[] ;
}"
`);
});
Expand Down

0 comments on commit a212798

Please sign in to comment.