medusa-plugin-ratings 1.0.0 → 1.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/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # Change Log
2
+
3
+ ## 1.2.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Update import paths for compatibility with @medusajs/medusa 1.12.0.
8
+
9
+ ## 1.1.0
10
+
11
+ ### Patch Changes
12
+
13
+ - Change typeorm peer dependency to typeorm 0.3.16
14
+ - Added zod schema checking on api endpoints
15
+ - Add customer and user authentication on some endpoints
package/README.md CHANGED
@@ -16,6 +16,12 @@ If you are not familiar with Medusa, you can learn more on [the project web site
16
16
  - By default, reviews are set with field `approved` to `false` (boolean)
17
17
  - Until Medusa adds extensibility for the admin app, approval must be done manually in the database.
18
18
 
19
+ ## Installation
20
+
21
+ ```bash
22
+ yarn add medusa-plugin-blog
23
+ ```
24
+
19
25
  ## Configuration
20
26
 
21
27
  Enable in your medusa-config.js file similar to other plugins:
@@ -26,3 +32,28 @@ const plugins = [
26
32
  ...
27
33
  ]
28
34
  ```
35
+
36
+ ## API Endpoints
37
+
38
+ ### `GET /store/products/:id/reviews`
39
+ returns a json object with an array of reviews for the product with the given id
40
+
41
+ ### `GET /store/customers/:id/reviews`
42
+ returns a json object with an array of reviews for the customer with the given id
43
+
44
+ ### `GET /store/reviews/:id`
45
+ returns a json object with the review with the given id
46
+
47
+ ### `POST /store/products/:id/reviews`
48
+ adds a review for the product with the given id. The body of the request should be a json object with the following fields:
49
+
50
+ ```js
51
+ {
52
+ customer_id: string!,
53
+ display_name: string!,
54
+ content: string!,
55
+ rating: number.min(0).max(5)!
56
+ }
57
+ ```
58
+
59
+
package/api/index.js ADDED
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ const cors_1 = __importDefault(require("cors"));
30
+ const config_1 = __importDefault(require("@medusajs/medusa/dist/loaders/config"));
31
+ const express_1 = require("express");
32
+ const bodyParser = __importStar(require("body-parser"));
33
+ //import { User, UserService, Customer, CustomerService } from "@medusajs/medusa"
34
+ const medusa_1 = require("@medusajs/medusa");
35
+ const utils_1 = require("@medusajs/utils");
36
+ const zod_1 = require("zod");
37
+ exports.default = (rootDirectory) => {
38
+ const config = (0, config_1.default)(rootDirectory);
39
+ const storeCorsOptions = { origin: config.projectConfig.store_cors.split(","), credentials: true, };
40
+ const adminCorsOptions = { origin: config.projectConfig.admin_cors.split(","), credentials: true, };
41
+ const router = (0, express_1.Router)();
42
+ // REVIEWS - GET ALL REVIEWS FOR A PRODUCT
43
+ //router.use("/store/products/:id/reviews", authenticateCustomer())
44
+ router.use((0, medusa_1.authenticateCustomer)());
45
+ //router.get("/store/products/:id/reviews", cors(storeCorsOptions), authenticateCustomer(), async (req, res) => {
46
+ router.get("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions), async (req, res) => {
47
+ // console.log('get reviews')
48
+ // console.log(req.user)
49
+ const productReviewService = req.scope.resolve("productReviewService");
50
+ productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
51
+ return res.json({ product_reviews });
52
+ });
53
+ });
54
+ // REVIEWS - GET ALL REVIEWS FOR A CUSTOMER
55
+ // router.use("/store/customers/:id/reviews", authenticate(), getCustomer)
56
+ router.get("/store/customers/:id/reviews", async (req, res) => {
57
+ const productReviewService = req.scope.resolve("productReviewService");
58
+ productReviewService.getCustomerProductReviews(req.params.id).then((product_reviews) => {
59
+ return res.json({ product_reviews });
60
+ });
61
+ });
62
+ // REVIEWS - ADD A NEW REVIEW FOR A PRODUCT
63
+ //router.use("/store/products/:id/reviews", authenticate(), getCustomer, bodyParser.json())
64
+ router.use("/store/products/:id/reviews", bodyParser.json());
65
+ router.post("/store/products/:id/reviews", async (req, res) => {
66
+ const schema = zod_1.z.object({
67
+ customer_id: zod_1.z.string().min(1),
68
+ display_name: zod_1.z.string().min(1),
69
+ content: zod_1.z.string().min(1),
70
+ rating: zod_1.z.coerce.number().min(0).max(5),
71
+ });
72
+ /* @ts-ignore */
73
+ const { success, error, data } = schema.safeParse(req.body);
74
+ if (!success) {
75
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, error);
76
+ }
77
+ else {
78
+ const productReviewService = req.scope.resolve("productReviewService");
79
+ productReviewService.addProductReview(req.params.id, data.customer_id, data.display_name, data.content, data.rating)
80
+ .then((product_review) => {
81
+ return res.json({ product_review });
82
+ });
83
+ }
84
+ });
85
+ // REVIEWS - GET A SINGLE REVIEW
86
+ router.get("/store/reviews/:id", async (req, res) => {
87
+ const productReviewService = req.scope.resolve("productReviewService");
88
+ productReviewService.getReview(req.params.id).then((product_review) => {
89
+ return res.json({ product_review });
90
+ });
91
+ });
92
+ // REVIEWS - UPDATE A REVIEW FOR A PRODUCT
93
+ //router.use("/store/reviews/:id", authenticate(), getCustomer, bodyParser.json())
94
+ router.use("/store/reviews/:id", bodyParser.json());
95
+ router.post("/store/reviews/:id", async (req, res) => {
96
+ const schema = zod_1.z.object({
97
+ customer_id: zod_1.z.string().min(1),
98
+ display_name: zod_1.z.string().min(1),
99
+ content: zod_1.z.string().min(1),
100
+ rating: zod_1.z.coerce.number().min(0).max(5),
101
+ });
102
+ /* @ts-ignore */
103
+ const { success, error, data } = schema.safeParse(req.body);
104
+ if (!success) {
105
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, error);
106
+ }
107
+ else {
108
+ }
109
+ const productReviewService = req.scope.resolve("productReviewService");
110
+ const productReview = await productReviewService.getProductReviews(req.params.id);
111
+ if (productReview.customer_id !== req.body.userId) {
112
+ return res.status(401).json({ message: "Unauthorized" });
113
+ }
114
+ productReviewService.updateProductReview(req.params.id, req.body.display_name, req.body.content, req.body.rating)
115
+ .then((product_review) => {
116
+ return res.json({ product_review });
117
+ });
118
+ });
119
+ // REVIEWS - ADMIN GET ALL REVIEWS FOR A PRODUCT
120
+ router.get("/admin/products/:id/reviews", async (req, res) => {
121
+ const productReviewService = req.scope.resolve("productReviewService");
122
+ productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
123
+ return res.json({ product_reviews });
124
+ });
125
+ });
126
+ // REVIEWS - ADMIN EDIT A REVIEW FOR A PRODUCT
127
+ //router.use("/admin/reviews/:id", bodyParser.json(), authenticate(), getUser)
128
+ router.use("/admin/reviews/:id", bodyParser.json());
129
+ router.post("/admin/reviews/:id", async (req, res) => {
130
+ const productReviewService = req.scope.resolve("productReviewService");
131
+ productReviewService.editProductReview(req.params.id, req.body.display_name, req.body.content, req.body.rating, req.body.approved)
132
+ .then((product_review) => {
133
+ return res.json({ product_review });
134
+ });
135
+ });
136
+ return router;
137
+ };
138
+ // async function getCustomer(req, res, next) {
139
+ // console.log('getCustomer')
140
+ // let loggedInCustomer: Customer | null = null
141
+ // if (req.customer && req.customer.customerId) {
142
+ // console.log(req.customer.customerId)
143
+ // const customerService = req.scope.resolve("customerService") as CustomerService
144
+ // loggedInCustomer = await customerService.retrieve(req.customer.customerId)
145
+ // }
146
+ // req.scope.register({ loggedInCustomer: { resolve: () => loggedInCustomer }})
147
+ // next()
148
+ // }
149
+ // async function getUser(req, res, next) {
150
+ // let loggedInUser: User | null = null
151
+ // if (req.user && req.user.userId) {
152
+ // const userService = req.scope.resolve("userService") as UserService
153
+ // loggedInUser = await userService.retrieve(req.user.userId)
154
+ // }
155
+ // req.scope.register({ loggedInUser: { resolve: () => loggedInUser }})
156
+ // next()
157
+ // }
158
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/api/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAuB;AACvB,kFAA+D;AAC/D,qCAAgC;AAChC,wDAAyC;AACzC,iFAAiF;AACjF,6CAAuD;AACvD,2CAA6C;AAC7C,6BAAuB;AAEvB,kBAAe,CAAC,aAAqB,EAAqB,EAAE;IAE3D,MAAM,MAAM,GAAG,IAAA,gBAAY,EAAC,aAAa,CAAC,CAAA;IAC1C,MAAM,gBAAgB,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,GAAG,CAAA;IACjG,MAAM,gBAAgB,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,IAAI,GAAG,CAAA;IAEnG,MAAM,MAAM,GAAG,IAAA,gBAAM,GAAE,CAAA;IAEvB,0CAA0C;IAC5C,mEAAmE;IACnE,MAAM,CAAC,GAAG,CAAC,IAAA,6BAAoB,GAAE,CAAC,CAAA;IAClC,iHAAiH;IACjH,MAAM,CAAC,GAAG,CAAC,6BAA6B,EA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E,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC;aACjI,IAAI,CAAC,CAAC,cAAc,EAAE,EAAE;YACtB,OAAO,GAAG,CAAC,IAAI,CAAC,EAAC,cAAc,EAAC,CAAC,CAAA;QACpC,CAAC,CAAC,CAAA;IACL,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AAChB,CAAC,CAAA;AAED,+CAA+C;AAC/C,6BAA6B;AAC7B,gDAAgD;AAChD,kDAAkD;AAClD,uCAAuC;AACvC,oFAAoF;AACpF,+EAA+E;AAC/E,KAAK;AACL,gFAAgF;AAChF,UAAU;AACV,IAAI;AAEJ,2CAA2C;AAC3C,wCAAwC;AACxC,sCAAsC;AACtC,wEAAwE;AACxE,+DAA+D;AAC/D,KAAK;AACL,wEAAwE;AACxE,UAAU;AACV,IAAI"}
@@ -27,3 +27,4 @@ class ProductReview1674838435739 {
27
27
  }
28
28
  }
29
29
  exports.ProductReview1674838435739 = ProductReview1674838435739;
30
+ //# sourceMappingURL=1674838435739-ProductReview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"1674838435739-ProductReview.js","sourceRoot":"","sources":["../src/migrations/1674838435739-ProductReview.ts"],"names":[],"mappings":";;;AAAA,qCAA0E;AAE1E,MAAa,0BAA0B;IAE5B,KAAK,CAAC,EAAE,CAAC,WAAwB;QACpC,MAAM,WAAW,CAAC,KAAK,CACnB;;;;;;;;0EAQ8D,CACjE,CAAA;QACD,MAAM,WAAW,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;QAC5D,MAAM,WAAW,CAAC,gBAAgB,CAAC,gBAAgB,EAAE,IAAI,yBAAe,CAAC;YACrE,WAAW,EAAE,CAAC,YAAY,CAAC;YAC3B,qBAAqB,EAAE,CAAC,IAAI,CAAC;YAC7B,mBAAmB,EAAE,SAAS;YAC9B,QAAQ,EAAE,SAAS;YACnB,QAAQ,EAAE,SAAS;SACtB,CAAC,CAAC,CAAA;IACP,CAAC;IAEM,KAAK,CAAC,IAAI,CAAC,WAAwB;QACtC,MAAM,WAAW,CAAC,SAAS,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAA;IACvD,CAAC;CAEJ;AA5BD,gEA4BC"}
@@ -14,15 +14,7 @@ const medusa_1 = require("@medusajs/medusa");
14
14
  const utils_1 = require("@medusajs/utils");
15
15
  const typeorm_1 = require("typeorm");
16
16
  const class_validator_1 = require("class-validator");
17
- let ProductReview = class ProductReview extends utils_1.BaseEntity {
18
- product_id;
19
- product;
20
- customer_id;
21
- customer;
22
- display_name;
23
- rating;
24
- content;
25
- approved;
17
+ let ProductReview = class ProductReview extends medusa_1.BaseEntity {
26
18
  beforeInsert() {
27
19
  this.id = (0, utils_1.generateEntityId)(this.id, "prev");
28
20
  }
@@ -42,7 +34,7 @@ __decorate([
42
34
  __metadata("design:type", String)
43
35
  ], ProductReview.prototype, "customer_id", void 0);
44
36
  __decorate([
45
- (0, typeorm_1.ManyToOne)(() => medusa_1.Product),
37
+ (0, typeorm_1.ManyToOne)(() => medusa_1.Customer),
46
38
  (0, typeorm_1.JoinColumn)({ name: "customer_id" }),
47
39
  __metadata("design:type", medusa_1.Customer)
48
40
  ], ProductReview.prototype, "customer", void 0);
@@ -74,3 +66,4 @@ ProductReview = __decorate([
74
66
  (0, typeorm_1.Entity)()
75
67
  ], ProductReview);
76
68
  exports.ProductReview = ProductReview;
69
+ //# sourceMappingURL=product-review.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product-review.js","sourceRoot":"","sources":["../src/models/product-review.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,6CAAgE;AAChE,2CAAkD;AAClD,qCAAoF;AACpF,qDAA0C;AAGnC,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,mBAAU;IAgCnC,YAAY;QAClB,IAAI,CAAC,EAAE,GAAG,IAAA,wBAAgB,EAAC,IAAI,CAAC,EAAE,EAAE,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reviews and ratings for Medusajs",
5
5
  "repository": {
6
6
  "type": "git",
@@ -18,40 +18,21 @@
18
18
  "medusa"
19
19
  ],
20
20
  "devDependencies": {
21
- "@babel/cli": "^7.7.5",
22
- "@babel/core": "^7.7.5",
23
- "@babel/node": "^7.7.4",
24
- "@babel/plugin-proposal-class-properties": "^7.7.4",
25
- "@babel/plugin-transform-instanceof": "^7.8.3",
26
- "@babel/plugin-transform-runtime": "^7.7.6",
27
- "@babel/preset-env": "^7.7.5",
28
- "@babel/register": "^7.7.4",
29
- "@babel/runtime": "^7.9.6",
30
- "awilix": "^8.0.0",
31
- "client-sessions": "^0.8.0",
32
21
  "cross-env": "^5.2.1",
33
- "jest": "^25.5.2",
34
- "medusa-core-utils": "^1.2.0",
35
- "medusa-test-utils": "^1.1.40"
22
+ "typeorm": "^0.3.16",
23
+ "typescript": "^4.4.4"
36
24
  },
37
25
  "scripts": {
38
26
  "prepare": "cross-env NODE_ENV=production yarn run build",
39
27
  "test": "jest --passWithNoTests src",
40
- "build": "babel src --out-dir . --ignore '**/__tests__','**/__mocks__'",
41
- "watch": "babel -w src --out-dir . --ignore '**/__tests__','**/__mocks__'"
28
+ "build": "tsc",
29
+ "watch": "tsc --watch"
42
30
  },
43
31
  "peerDependencies": {
44
- "@medusajs/medusa": "^1.10.0",
45
- "medusa-interfaces": "1.3.7",
46
- "typeorm": "npm:@medusajs/typeorm@next",
47
- "@medusajs/utils": "1.8.3"
32
+ "@medusajs/medusa": "^1.12.0",
33
+ "@medusajs/utils": "1.9.0"
48
34
  },
49
35
  "dependencies": {
50
- "@babel/plugin-transform-classes": "^7.9.5",
51
- "body-parser": "^1.19.0",
52
- "class-validator": "^0.14.0",
53
- "cors": "^2.8.5",
54
- "express": "^4.17.1",
55
36
  "zod": "^3.21.4"
56
37
  }
57
38
  }
@@ -4,3 +4,4 @@ exports.ProductReviewRepository = void 0;
4
4
  const product_review_1 = require("../models/product-review");
5
5
  const database_1 = require("@medusajs/medusa/dist/loaders/database");
6
6
  exports.ProductReviewRepository = database_1.dataSource.getRepository(product_review_1.ProductReview);
7
+ //# sourceMappingURL=product-review.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product-review.js","sourceRoot":"","sources":["../src/repositories/product-review.ts"],"names":[],"mappings":";;;AAAA,6DAAwD;AACxD,qEAAmE;AAEtD,QAAA,uBAAuB,GAAG,qBAAU,CAAC,aAAa,CAAC,8BAAa,CAAC,CAAA"}
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const utils_1 = require("@medusajs/utils");
4
- class ProductReviewService extends utils_1.TransactionBaseService {
5
- productReviewRepository_;
3
+ const medusa_1 = require("@medusajs/medusa");
4
+ class ProductReviewService extends medusa_1.TransactionBaseService {
6
5
  constructor({ productReviewRepository }) {
7
6
  super(arguments[0]);
8
7
  this.productReviewRepository_ = productReviewRepository;
9
8
  }
10
9
  async getProductReviews(product_id) {
10
+ /* @ts-ignore */
11
11
  const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
12
12
  return await productReviewRepository.find({
13
13
  where: {
@@ -17,6 +17,7 @@ class ProductReviewService extends utils_1.TransactionBaseService {
17
17
  });
18
18
  }
19
19
  async getCustomerProductReviews(customer_id) {
20
+ /* @ts-ignore */
20
21
  const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
21
22
  return await productReviewRepository.find({
22
23
  where: {
@@ -25,34 +26,59 @@ class ProductReviewService extends utils_1.TransactionBaseService {
25
26
  }
26
27
  });
27
28
  }
29
+ async getReview(id) {
30
+ /* @ts-ignore */
31
+ const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
32
+ return await productReviewRepository.findOne({
33
+ where: {
34
+ id
35
+ }
36
+ });
37
+ }
28
38
  async addProductReview(product_id, customer_id, display_name, content, rating) {
29
39
  if (!product_id || !customer_id || !display_name || !content || !rating) {
30
- throw new Error("product review requires product_id, customer_id, display_name, content, and rating");
40
+ throw new Error("adding product review requires product_id, customer_id, display_name, content, and rating");
31
41
  }
42
+ /* @ts-ignore */
32
43
  const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
33
44
  const createdReview = productReviewRepository.create({
34
- product_id: product_id,
35
- customer_id: customer_id,
36
- display_name: display_name,
37
- content: content,
38
- rating: rating,
45
+ product_id,
46
+ customer_id,
47
+ display_name,
48
+ content,
49
+ rating,
39
50
  approved: false
40
51
  });
41
52
  const productReview = await productReviewRepository.save(createdReview);
42
53
  return productReview;
43
54
  }
55
+ async updateProductReview(id, display_name, content, rating) {
56
+ if (!id || !display_name || !content || !rating) {
57
+ throw new Error("updating a product review requires id, display_name, content, rating, and approved");
58
+ }
59
+ /* @ts-ignore */
60
+ const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
61
+ const productReview = productReviewRepository.update(id, {
62
+ display_name,
63
+ content,
64
+ rating
65
+ });
66
+ return productReview;
67
+ }
44
68
  async editProductReview(id, display_name, content, rating, approved) {
45
69
  if (!id || !display_name || !content || !rating || !approved) {
46
70
  throw new Error("updating a product review requires id, display_name, content, rating, and approved");
47
71
  }
72
+ /* @ts-ignore */
48
73
  const productReviewRepository = this.activeManager_.withRepository(this.productReviewRepository_);
49
74
  const productReview = productReviewRepository.update(id, {
50
- display_name: display_name,
51
- content: content,
52
- rating: rating,
53
- approved: approved
75
+ display_name,
76
+ content,
77
+ rating,
78
+ approved
54
79
  });
55
80
  return productReview;
56
81
  }
57
82
  }
58
83
  exports.default = ProductReviewService;
84
+ //# sourceMappingURL=product-review.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product-review.js","sourceRoot":"","sources":["../src/services/product-review.ts"],"names":[],"mappings":";;AAAA,6CAAyD;AAGzD,MAAqB,oBAAqB,SAAQ,+BAAsB;IAGvE,YAAY,EAAE,uBAAuB,EAAE;QACtC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAA;QACnB,IAAI,CAAC,wBAAwB,GAAG,uBAAuB,CAAA;IACxD,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 -1,25 +1,29 @@
1
1
  {
2
- "compilerOptions": {
3
- "lib": ["es5", "es6"],
4
- "target": "esnext",
5
- "allowJs": true,
6
- "esModuleInterop": true,
7
- "module": "commonjs",
8
- "moduleResolution": "node",
9
- "emitDecoratorMetadata": true,
10
- "experimentalDecorators": true,
11
- "skipLibCheck": true,
12
- "skipDefaultLibCheck": true,
13
- "declaration": false,
14
- "sourceMap": false,
15
- "outDir": "./dist",
16
- "rootDir": "src",
17
- "baseUrl": "src"
18
- },
19
- "include": ["src"],
20
- "exclude": [
21
- "**/__tests__",
22
- "**/__fixtures__",
23
- "node_modules"
24
- ]
25
- }
2
+ "compilerOptions": {
3
+ "lib": ["es2020"],
4
+ "target": "es2020",
5
+ "outDir": "./",
6
+ "esModuleInterop": true,
7
+ "declaration": false,
8
+ "module": "commonjs",
9
+ "moduleResolution": "node",
10
+ "emitDecoratorMetadata": true,
11
+ "experimentalDecorators": true,
12
+ "sourceMap": true,
13
+ "noImplicitReturns": false,
14
+ "strictNullChecks": false,
15
+ "strictFunctionTypes": false,
16
+ "noImplicitThis": false,
17
+ "allowJs": true,
18
+ "skipLibCheck": true,
19
+ "downlevelIteration": true
20
+ },
21
+ "include": ["src"],
22
+ "exclude": [
23
+ "dist",
24
+ "src/**/__tests__",
25
+ "src/**/__mocks__",
26
+ "src/**/__fixtures__",
27
+ "node_modules"
28
+ ]
29
+ }
package/dist/api/index.js DELETED
@@ -1,58 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const express_1 = require("express");
7
- const medusa_core_utils_1 = require("medusa-core-utils");
8
- const cors_1 = __importDefault(require("cors"));
9
- const body_parser_1 = __importDefault(require("body-parser"));
10
- exports.default = (rootDirectory) => {
11
- const { configModule } = (0, medusa_core_utils_1.getConfigFile)(rootDirectory, "medusa-config");
12
- const { projectConfig } = configModule;
13
- const storeCorsOptions = {
14
- origin: projectConfig.store_cors.split(","),
15
- credentials: true,
16
- };
17
- const adminCorsOptions = {
18
- origin: projectConfig.admin_cors.split(","),
19
- credentials: true,
20
- };
21
- const router = (0, express_1.Router)();
22
- router.get("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
23
- const productReviewService = req.scope.resolve("productReviewService");
24
- productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
25
- return res.json({
26
- product_reviews
27
- });
28
- });
29
- });
30
- router.get("/store/customers/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
31
- const productReviewService = req.scope.resolve("productReviewService");
32
- productReviewService.getCustomerProductReviews(req.params.id).then((product_reviews) => {
33
- return res.json({
34
- product_reviews
35
- });
36
- });
37
- });
38
- router.use(body_parser_1.default.json());
39
- router.options("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions));
40
- router.post("/store/products/:id/reviews", (0, cors_1.default)(storeCorsOptions), (req, res) => {
41
- const productReviewService = req.scope.resolve("productReviewService");
42
- productReviewService.addProductReview(req.params.id, req.body.customer_id, req.body.display_name, req.body.content, req.body.rating).then((product_review) => {
43
- return res.json({
44
- product_review
45
- });
46
- });
47
- });
48
- router.options("/admin/products/:id/reviews", (0, cors_1.default)(adminCorsOptions));
49
- router.get("/admin/products/:id/reviews", (0, cors_1.default)(adminCorsOptions), async (req, res) => {
50
- const productReviewService = req.scope.resolve("productReviewService");
51
- productReviewService.getProductReviews(req.params.id).then((product_reviews) => {
52
- return res.json({
53
- product_reviews
54
- });
55
- });
56
- });
57
- return router;
58
- };
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "include": ["src"],
4
- "exclude": ["dist", "node_modules"]
5
- }