@haus-tech/package-size-plugin 3.0.1 → 3.0.2
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 +177 -32
- package/dist/api/api-extensions.d.ts +1 -1
- package/dist/api/api-extensions.js +11 -7
- package/dist/package-size.plugin.d.ts +4 -1
- package/dist/package-size.plugin.js +49 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,59 +1,204 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Package Size Plugin for Vendure
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
2. Update the `name` and `description` field in `package.json`
|
|
5
|
-
3. Update this Readme: What does the plugin do? How can someone use your plugin in their project?
|
|
6
|
-
4. Run `yarn` to install the dependencies
|
|
7
|
-
5. Run `yarn start` to start the server
|
|
3
|
+
This plugin adds package size and optional unit functionality to product variants in Vendure.
|
|
8
4
|
|
|
9
|
-
|
|
5
|
+
## Installation
|
|
10
6
|
|
|
11
|
-
|
|
7
|
+
```bash
|
|
8
|
+
yarn add @haus-tech/package-size-plugin
|
|
9
|
+
```
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
## Basic Usage
|
|
12
|
+
|
|
13
|
+
The plugin must be initialized with configuration options using the `.init()` method:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { VendureConfig } from '@vendure/core'
|
|
17
|
+
import { PackageSizePlugin } from '@haus-tech/package-size-plugin'
|
|
18
|
+
|
|
19
|
+
export const config: VendureConfig = {
|
|
20
|
+
// ... other config
|
|
21
|
+
plugins: [
|
|
22
|
+
PackageSizePlugin.init({
|
|
23
|
+
// Configuration options (see below)
|
|
24
|
+
}),
|
|
25
|
+
// ... other plugins
|
|
26
|
+
],
|
|
16
27
|
}
|
|
17
28
|
```
|
|
18
29
|
|
|
19
|
-
##
|
|
30
|
+
## Configuration Options
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
2. Don't forget to implement your own!
|
|
32
|
+
### Basic Configuration (Package Size Only)
|
|
23
33
|
|
|
24
|
-
|
|
34
|
+
```typescript
|
|
35
|
+
PackageSizePlugin.init({
|
|
36
|
+
// Uses default settings: integer package size, no unit field
|
|
37
|
+
})
|
|
38
|
+
```
|
|
25
39
|
|
|
26
|
-
|
|
27
|
-
2. `yarn build`
|
|
28
|
-
3. `yarn publish`
|
|
40
|
+
This creates a single custom field:
|
|
29
41
|
|
|
30
|
-
|
|
42
|
+
- `packageSizeInternal`: Integer field with default value `1`
|
|
31
43
|
|
|
32
|
-
|
|
44
|
+
### Float Package Size Support
|
|
33
45
|
|
|
34
|
-
|
|
46
|
+
```typescript
|
|
47
|
+
PackageSizePlugin.init({
|
|
48
|
+
packageSizeFieldType: 'float',
|
|
49
|
+
})
|
|
50
|
+
```
|
|
35
51
|
|
|
36
|
-
|
|
37
|
-
2. Check out [GraphQL codegen](https://the-guild.dev/graphql/codegen) to generate Typescript types for your custom GraphQL types
|
|
52
|
+
This allows decimal values like `0.5`, `1.25`, `2.75`, etc.
|
|
38
53
|
|
|
39
|
-
|
|
40
|
-
- Add following to elasticSearchConfig
|
|
54
|
+
### With Package Size Unit
|
|
41
55
|
|
|
42
|
-
|
|
56
|
+
```typescript
|
|
57
|
+
PackageSizePlugin.init({
|
|
58
|
+
usePackageSizeUnit: true,
|
|
59
|
+
defaultUnit: 'kg',
|
|
60
|
+
})
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This adds both fields:
|
|
64
|
+
|
|
65
|
+
- `packageSizeInternal`: Integer package size
|
|
66
|
+
- `packageSizeUnitInternal`: String unit field with text input
|
|
43
67
|
|
|
68
|
+
### Full Configuration
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
PackageSizePlugin.init({
|
|
72
|
+
packageSizeFieldType: 'float', // Enable decimal values
|
|
73
|
+
usePackageSizeUnit: true, // Enable unit field
|
|
74
|
+
defaultUnit: 'kg', // Set default unit
|
|
75
|
+
})
|
|
44
76
|
```
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
77
|
+
|
|
78
|
+
## Configuration Reference
|
|
79
|
+
|
|
80
|
+
| Option | Type | Default | Description |
|
|
81
|
+
| ---------------------- | ------------------ | ------- | ------------------------------------------------------------------------ |
|
|
82
|
+
| `packageSizeFieldType` | `'int' \| 'float'` | `'int'` | Data type for package size field |
|
|
83
|
+
| `usePackageSizeUnit` | `boolean` | `false` | Whether to include the unit field |
|
|
84
|
+
| `defaultUnit` | `string` | `'st'` | Default value for unit field (only used if `usePackageSizeUnit` is true) |
|
|
85
|
+
|
|
86
|
+
## GraphQL API
|
|
87
|
+
|
|
88
|
+
The plugin dynamically adds fields to the ProductVariant type based on your configuration:
|
|
89
|
+
|
|
90
|
+
### Integer Package Size Only
|
|
91
|
+
|
|
92
|
+
```graphql
|
|
93
|
+
type ProductVariant {
|
|
94
|
+
packageSize: Int
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Float Package Size with Unit
|
|
99
|
+
|
|
100
|
+
```graphql
|
|
101
|
+
type ProductVariant {
|
|
102
|
+
packageSize: Float
|
|
103
|
+
packageSizeUnit: String
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Migration from Previous Versions
|
|
108
|
+
|
|
109
|
+
If you were using this plugin without the `.init()` method, you'll need to update your configuration:
|
|
110
|
+
|
|
111
|
+
**Before:**
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
plugins: [PackageSizePlugin]
|
|
48
115
|
```
|
|
49
116
|
|
|
50
|
-
|
|
117
|
+
**After:**
|
|
51
118
|
|
|
119
|
+
```typescript
|
|
120
|
+
plugins: [
|
|
121
|
+
PackageSizePlugin.init({
|
|
122
|
+
// Add your desired configuration
|
|
123
|
+
packageSizeFieldType: 'int', // or 'float'
|
|
124
|
+
usePackageSizeUnit: false, // or true if you want units
|
|
125
|
+
}),
|
|
126
|
+
]
|
|
52
127
|
```
|
|
128
|
+
|
|
129
|
+
## Database Migration
|
|
130
|
+
|
|
131
|
+
When changing `packageSizeFieldType` from `'int'` to `'float'`, you'll need to create a database migration:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
npx vendure migration:generate update-package-size-to-float
|
|
135
|
+
npx vendure migration:run
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Elasticsearch Integration
|
|
139
|
+
|
|
140
|
+
If you're using Elasticsearch, configure the mappings based on your field type:
|
|
141
|
+
|
|
142
|
+
### For Integer Package Size
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const elasticSearchConfig: ElasticsearchOptions = {
|
|
146
|
+
// ... other config
|
|
147
|
+
indexMappingProperties: {
|
|
148
|
+
'variant-packageSize': { type: 'integer' },
|
|
149
|
+
'variant-packageSizeUnit': { type: 'keyword' }, // if using units
|
|
150
|
+
},
|
|
151
|
+
customProductVariantMappings: {
|
|
53
152
|
packageSize: {
|
|
54
153
|
graphQlType: 'Int!',
|
|
55
|
-
valueFn: async (productVariant: ProductVariant
|
|
56
|
-
return productVariant.customFields.packageSizeInternal
|
|
154
|
+
valueFn: async (productVariant: ProductVariant) => {
|
|
155
|
+
return productVariant.customFields.packageSizeInternal ?? 1
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
packageSizeUnit: {
|
|
159
|
+
// if using units
|
|
160
|
+
graphQlType: 'String!',
|
|
161
|
+
valueFn: async (productVariant: ProductVariant) => {
|
|
162
|
+
return productVariant.customFields.packageSizeUnitInternal ?? 'st'
|
|
57
163
|
},
|
|
58
164
|
},
|
|
165
|
+
},
|
|
166
|
+
}
|
|
59
167
|
```
|
|
168
|
+
|
|
169
|
+
### For Float Package Size
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
const elasticSearchConfig: ElasticsearchOptions = {
|
|
173
|
+
// ... other config
|
|
174
|
+
indexMappingProperties: {
|
|
175
|
+
'variant-packageSize': { type: 'float' },
|
|
176
|
+
'variant-packageSizeUnit': { type: 'keyword' }, // if using units
|
|
177
|
+
},
|
|
178
|
+
customProductVariantMappings: {
|
|
179
|
+
packageSize: {
|
|
180
|
+
graphQlType: 'Float!',
|
|
181
|
+
valueFn: async (productVariant: ProductVariant) => {
|
|
182
|
+
return productVariant.customFields.packageSizeInternal ?? 1.0
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
packageSizeUnit: {
|
|
186
|
+
// if using units
|
|
187
|
+
graphQlType: 'String!',
|
|
188
|
+
valueFn: async (productVariant: ProductVariant) => {
|
|
189
|
+
return productVariant.customFields.packageSizeUnitInternal ?? 'st'
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## Features
|
|
197
|
+
|
|
198
|
+
- ✅ Configurable package size field type (integer or float)
|
|
199
|
+
- ✅ Optional package size unit field
|
|
200
|
+
- ✅ Dynamic GraphQL schema generation
|
|
201
|
+
- ✅ Elasticsearch integration support
|
|
202
|
+
- ✅ Multi-language labels (English/Swedish)
|
|
203
|
+
- ✅ TypeScript support
|
|
204
|
+
- ✅ Backward compatibility with migration path
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const getShopApiExtensions: (packageSizeFieldType: "int" | "float") => import("graphql").DocumentNode;
|
|
@@ -3,11 +3,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.getShopApiExtensions = void 0;
|
|
7
7
|
const graphql_tag_1 = __importDefault(require("graphql-tag"));
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
const getShopApiExtensions = (packageSizeFieldType) => {
|
|
9
|
+
const packageSizeType = packageSizeFieldType === 'float' ? 'Float' : 'Int';
|
|
10
|
+
return (0, graphql_tag_1.default) `
|
|
11
|
+
extend type ProductVariant {
|
|
12
|
+
packageSize: ${packageSizeType}
|
|
13
|
+
packageSizeUnit: String
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
};
|
|
17
|
+
exports.getShopApiExtensions = getShopApiExtensions;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LanguageCode } from '@vendure/core';
|
|
1
|
+
import { LanguageCode, Type } from '@vendure/core';
|
|
2
2
|
export interface PackageSizeUnit {
|
|
3
3
|
value: string;
|
|
4
4
|
label: {
|
|
@@ -8,6 +8,8 @@ export interface PackageSizeUnit {
|
|
|
8
8
|
}
|
|
9
9
|
export interface PackageSizePluginConfig {
|
|
10
10
|
defaultUnit?: string;
|
|
11
|
+
packageSizeFieldType?: 'int' | 'float';
|
|
12
|
+
usePackageSizeUnit?: boolean;
|
|
11
13
|
}
|
|
12
14
|
declare module '@vendure/core' {
|
|
13
15
|
interface CustomProductVariantFields {
|
|
@@ -20,4 +22,5 @@ declare module '@vendure/core' {
|
|
|
20
22
|
}
|
|
21
23
|
}
|
|
22
24
|
export declare class PackageSizePlugin {
|
|
25
|
+
static init(config: PackageSizePluginConfig): Type<any>;
|
|
23
26
|
}
|
|
@@ -10,43 +10,53 @@ exports.PackageSizePlugin = void 0;
|
|
|
10
10
|
const core_1 = require("@vendure/core");
|
|
11
11
|
const api_extensions_1 = require("./api/api-extensions");
|
|
12
12
|
const package_size_resolver_1 = require("./api/package-size.resolver");
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const defaultUnit = pluginConfig.defaultUnit || 'st';
|
|
26
|
-
config.customFields.ProductVariant.push({
|
|
27
|
-
name: 'packageSizeInternal',
|
|
28
|
-
type: 'int',
|
|
29
|
-
defaultValue: 1,
|
|
30
|
-
public: false,
|
|
31
|
-
label: [
|
|
32
|
-
{ languageCode: core_1.LanguageCode.en, value: 'Package Size' },
|
|
33
|
-
{ languageCode: core_1.LanguageCode.sv, value: 'Förpackningsstorlek' },
|
|
34
|
-
],
|
|
35
|
-
}, {
|
|
36
|
-
name: 'packageSizeUnitInternal',
|
|
37
|
-
type: 'string',
|
|
38
|
-
defaultValue: defaultUnit,
|
|
39
|
-
ui: {
|
|
40
|
-
component: 'text-form-input',
|
|
13
|
+
class PackageSizePlugin {
|
|
14
|
+
static init(config) {
|
|
15
|
+
const fieldType = config.packageSizeFieldType || 'int';
|
|
16
|
+
const usePackageSizeUnit = config.usePackageSizeUnit || false;
|
|
17
|
+
let DynamicPackageSizePlugin = class DynamicPackageSizePlugin {
|
|
18
|
+
};
|
|
19
|
+
DynamicPackageSizePlugin = __decorate([
|
|
20
|
+
(0, core_1.VendurePlugin)({
|
|
21
|
+
imports: [core_1.PluginCommonModule],
|
|
22
|
+
shopApiExtensions: {
|
|
23
|
+
resolvers: [package_size_resolver_1.ProductVariantEntityResolver],
|
|
24
|
+
schema: (0, api_extensions_1.getShopApiExtensions)(fieldType),
|
|
41
25
|
},
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
26
|
+
configuration: (vendureConfig) => {
|
|
27
|
+
const defaultUnit = config.defaultUnit || 'st';
|
|
28
|
+
const defaultValue = fieldType === 'float' ? 1.0 : 1;
|
|
29
|
+
vendureConfig.customFields.ProductVariant.push({
|
|
30
|
+
name: 'packageSizeInternal',
|
|
31
|
+
type: fieldType,
|
|
32
|
+
defaultValue: defaultValue,
|
|
33
|
+
public: false,
|
|
34
|
+
label: [
|
|
35
|
+
{ languageCode: core_1.LanguageCode.en, value: 'Package Size' },
|
|
36
|
+
{ languageCode: core_1.LanguageCode.sv, value: 'Förpackningsstorlek' },
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
if (usePackageSizeUnit) {
|
|
40
|
+
vendureConfig.customFields.ProductVariant.push({
|
|
41
|
+
name: 'packageSizeUnitInternal',
|
|
42
|
+
type: 'string',
|
|
43
|
+
defaultValue: defaultUnit,
|
|
44
|
+
ui: {
|
|
45
|
+
component: 'text-form-input',
|
|
46
|
+
},
|
|
47
|
+
public: false,
|
|
48
|
+
label: [
|
|
49
|
+
{ languageCode: core_1.LanguageCode.en, value: 'Package Size Unit' },
|
|
50
|
+
{ languageCode: core_1.LanguageCode.sv, value: 'Förpackningsenhet' },
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return vendureConfig;
|
|
55
|
+
},
|
|
56
|
+
compatibility: '^3.0.0',
|
|
57
|
+
})
|
|
58
|
+
], DynamicPackageSizePlugin);
|
|
59
|
+
return DynamicPackageSizePlugin;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.PackageSizePlugin = PackageSizePlugin;
|