@goldenhippo/builder-cart-schemas 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +107 -41
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
# @goldenhippo/builder-cart-schemas
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Builder.io model definitions and TypeScript content types for Golden Hippo's cart/commerce integration. Provides factory functions for creating Builder.io content models and strongly-typed interfaces for consuming model data.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Installation](#installation)
|
|
8
|
+
- [Using Content Types](#using-content-types)
|
|
9
|
+
- [Fetching typed content in Angular](#fetching-typed-content-in-angular)
|
|
10
|
+
- [Working with page types](#working-with-page-types)
|
|
11
|
+
- [Using core types](#using-core-types)
|
|
12
|
+
- [Exports](#exports)
|
|
13
|
+
- [Models](#models)
|
|
14
|
+
- [Data Models](#data-models)
|
|
15
|
+
- [Page Models](#page-models)
|
|
16
|
+
- [Section Models](#section-models)
|
|
17
|
+
- [Creating Model Definitions](#creating-model-definitions)
|
|
18
|
+
- [Model Dependencies](#model-dependencies)
|
|
19
|
+
- [Development](#development)
|
|
4
20
|
|
|
5
21
|
## Installation
|
|
6
22
|
|
|
@@ -8,6 +24,94 @@ Shared TypeScript types and Builder.io model definitions for Golden Hippo's cart
|
|
|
8
24
|
npm install @goldenhippo/builder-cart-schemas
|
|
9
25
|
```
|
|
10
26
|
|
|
27
|
+
## Using Content Types
|
|
28
|
+
|
|
29
|
+
Content types represent the shape of data returned from Builder.io's Content API. Use them to get type-safe access to model fields when fetching content.
|
|
30
|
+
|
|
31
|
+
### Fetching typed content in Angular
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { Component, OnInit } from '@angular/core';
|
|
35
|
+
import { fetchOneEntry, fetchEntries } from '@builder.io/sdk-angular';
|
|
36
|
+
import type { BuilderProductContent, BuilderBlogCategoryContent } from '@goldenhippo/builder-cart-schemas';
|
|
37
|
+
import type { BuilderPageContent } from '@goldenhippo/builder-cart-schemas/page';
|
|
38
|
+
|
|
39
|
+
@Component({ selector: 'app-product', templateUrl: './product.component.html' })
|
|
40
|
+
export class ProductComponent implements OnInit {
|
|
41
|
+
product: BuilderProductContent | null = null;
|
|
42
|
+
|
|
43
|
+
async ngOnInit() {
|
|
44
|
+
const result = await fetchOneEntry({
|
|
45
|
+
model: 'product',
|
|
46
|
+
apiKey: environment.builderApiKey,
|
|
47
|
+
query: { 'data.gh.slug': 'example-product' },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
this.product = result as BuilderProductContent;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get displayName(): string {
|
|
54
|
+
return this.product?.data?.displayName ?? '';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get featuredImage(): string {
|
|
58
|
+
return this.product?.data?.featuredImage ?? '';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get averageRating(): number {
|
|
62
|
+
return this.product?.data?.reviews?.averageRating ?? 0;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// Fetching multiple entries
|
|
69
|
+
const categories = (await fetchEntries({
|
|
70
|
+
model: 'blog-category',
|
|
71
|
+
apiKey: environment.builderApiKey,
|
|
72
|
+
})) as BuilderBlogCategoryContent[];
|
|
73
|
+
|
|
74
|
+
categories.forEach((cat) => {
|
|
75
|
+
console.log(cat?.data?.name, cat?.data?.displayName);
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Working with page types
|
|
80
|
+
|
|
81
|
+
Page content uses a discriminated union based on `pageType`. TypeScript narrows the type automatically:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import type {
|
|
85
|
+
BuilderPageContent,
|
|
86
|
+
BuilderPdpPageContent,
|
|
87
|
+
BuilderBlogPageContent,
|
|
88
|
+
} from '@goldenhippo/builder-cart-schemas/page';
|
|
89
|
+
|
|
90
|
+
function renderPage(page: BuilderPageContent) {
|
|
91
|
+
const pageType = page?.data?.pageType;
|
|
92
|
+
|
|
93
|
+
if (pageType === 'Product') {
|
|
94
|
+
const pdp = page as BuilderPdpPageContent;
|
|
95
|
+
console.log(pdp?.data?.pdp?.product?.name);
|
|
96
|
+
console.log(pdp?.data?.pdp?.offerSelector?.osType);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (pageType === 'Blog') {
|
|
100
|
+
const blog = page as BuilderBlogPageContent;
|
|
101
|
+
console.log(blog?.data?.blog?.author);
|
|
102
|
+
console.log(blog?.data?.blog?.publicationDate);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Using core types
|
|
108
|
+
|
|
109
|
+
Core types like `ModelShape` and `BuilderIOFieldTypes` are available from the `@goldenhippo/builder-types` package:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import type { ModelShape, BuilderIOFieldTypes } from '@goldenhippo/builder-types';
|
|
113
|
+
```
|
|
114
|
+
|
|
11
115
|
## Exports
|
|
12
116
|
|
|
13
117
|
The package provides multiple entry points for granular imports:
|
|
@@ -19,8 +123,6 @@ The package provides multiple entry points for granular imports:
|
|
|
19
123
|
| `@goldenhippo/builder-cart-schemas/page` | Page model creator and content types |
|
|
20
124
|
| `@goldenhippo/builder-cart-schemas/section` | Section/component model creators and content types |
|
|
21
125
|
|
|
22
|
-
Core types (`ModelShape`, `BuilderIOFieldTypes`, etc.) are available from `@goldenhippo/builder-types`.
|
|
23
|
-
|
|
24
126
|
## Models
|
|
25
127
|
|
|
26
128
|
### Data Models
|
|
@@ -28,6 +130,7 @@ Core types (`ModelShape`, `BuilderIOFieldTypes`, etc.) are available from `@gold
|
|
|
28
130
|
| Model | Creator | Content Type |
|
|
29
131
|
| ------------------------- | ---------------------------------------------------------- | -------------------------------------- |
|
|
30
132
|
| Product | `createProductModel(props)` | `BuilderProductContent` |
|
|
133
|
+
| Product Group | `createProductGroupModel(props)` | `BuilderProductGroupContent` |
|
|
31
134
|
| Product Category | `createCategoryModel()` | `BuilderProductCategoryContent` |
|
|
32
135
|
| Product Tag | `createProductTagModel()` | `BuilderProductTagContent` |
|
|
33
136
|
| Product Ingredient | `createIngredientsModel()` | `BuilderIngredientContent` |
|
|
@@ -50,9 +153,7 @@ Core types (`ModelShape`, `BuilderIOFieldTypes`, etc.) are available from `@gold
|
|
|
50
153
|
| Site Banner | `createSiteBannerModel(editUrl)` | `BuilderSiteBannerModelContent` |
|
|
51
154
|
| Default Website Section | `createDefaultWebsiteSectionModel(editUrl)` | `BuilderDefaultWebsiteSectionContent` |
|
|
52
155
|
|
|
53
|
-
##
|
|
54
|
-
|
|
55
|
-
### Creating model definitions
|
|
156
|
+
## Creating Model Definitions
|
|
56
157
|
|
|
57
158
|
Model creators return a `ModelShape` object suitable for registration with Builder.io's API. Some models require IDs of other models they reference.
|
|
58
159
|
|
|
@@ -81,32 +182,6 @@ const pageModel = createPageModel({
|
|
|
81
182
|
});
|
|
82
183
|
```
|
|
83
184
|
|
|
84
|
-
### Using content types
|
|
85
|
-
|
|
86
|
-
Content types extend `BuilderContent` from `@builder.io/sdk` and represent the shape of data returned from Builder.io's Content API.
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
import type { BuilderProductContent, BuilderPageContent } from '@goldenhippo/builder-cart-schemas';
|
|
90
|
-
|
|
91
|
-
// Type-safe access to product content
|
|
92
|
-
function getProductName(product: BuilderProductContent): string {
|
|
93
|
-
return product?.data?.displayName ?? product?.data?.name ?? '';
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Page content is a union type based on page type
|
|
97
|
-
function handlePage(page: BuilderPageContent) {
|
|
98
|
-
if (page?.data?.pageType === 'Product') {
|
|
99
|
-
// TypeScript narrows to BuilderPdpPageContent
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
### Using core types
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
import type { ModelShape, BuilderIOFieldTypes } from '@goldenhippo/builder-types';
|
|
108
|
-
```
|
|
109
|
-
|
|
110
185
|
## Model Dependencies
|
|
111
186
|
|
|
112
187
|
Models that reference other models require their Builder.io model IDs at creation time:
|
|
@@ -130,12 +205,3 @@ npm run typecheck # Type-check with tsc
|
|
|
130
205
|
npm run test # Run tests with vitest
|
|
131
206
|
npm run lint # Lint with eslint
|
|
132
207
|
```
|
|
133
|
-
|
|
134
|
-
## Build Output
|
|
135
|
-
|
|
136
|
-
Built with [tsup](https://tsup.egoist.dev/) producing:
|
|
137
|
-
|
|
138
|
-
- **CJS** (`.cjs`) — CommonJS for Node.js / legacy bundlers
|
|
139
|
-
- **ESM** (`.js`) — ES modules for modern bundlers
|
|
140
|
-
- **Declarations** (`.d.ts`, `.d.cts`) — TypeScript type definitions
|
|
141
|
-
- **Source maps** (`.map`) — for debugging
|
package/package.json
CHANGED