Skip to content

Commit 5ca046d

Browse files
committed
Chore: Improve typing
1 parent 0718c4d commit 5ca046d

11 files changed

Lines changed: 261 additions & 49 deletions

File tree

packages/evershop/src/lib/util/registry.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isEqual from 'react-fast-compare';
2+
import type { ValueRegistry } from '../../types/registry.js';
23

34
let locked = false;
45

@@ -280,6 +281,22 @@ export function getValueSync<T>(
280281
return val;
281282
}
282283

284+
/**
285+
* Add a processor for a known registry value (typed).
286+
*/
287+
export function addProcessor<K extends keyof ValueRegistry>(
288+
name: K,
289+
callback: SyncProcessor<ValueRegistry[K]> | AsyncProcessor<ValueRegistry[K]>,
290+
priority?: number
291+
): void;
292+
/**
293+
* Add a processor for a custom registry value.
294+
*/
295+
export function addProcessor<T = unknown>(
296+
name: string,
297+
callback: SyncProcessor<T> | AsyncProcessor<T>,
298+
priority?: number
299+
): void;
283300
export function addProcessor<T>(
284301
name: string,
285302
callback: SyncProcessor<T> | AsyncProcessor<T>,
@@ -288,6 +305,20 @@ export function addProcessor<T>(
288305
return registry.addProcessor(name, callback, priority);
289306
}
290307

308+
/**
309+
* Add a final (priority-1000) processor for a known registry value (typed).
310+
*/
311+
export function addFinalProcessor<K extends keyof ValueRegistry>(
312+
name: K,
313+
callback: SyncProcessor<ValueRegistry[K]> | AsyncProcessor<ValueRegistry[K]>
314+
): void;
315+
/**
316+
* Add a final (priority-1000) processor for a custom registry value.
317+
*/
318+
export function addFinalProcessor<T = unknown>(
319+
name: string,
320+
callback: SyncProcessor<T> | AsyncProcessor<T>
321+
): void;
291322
export function addFinalProcessor(
292323
name: string,
293324
callback: SyncProcessor<any> | AsyncProcessor<any>

packages/evershop/src/modules/cms/api/deleteCmsPage/deleteCmsPage.js renamed to packages/evershop/src/modules/cms/api/deleteCmsPage/deleteCmsPage.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { INTERNAL_SERVER_ERROR, OK } from '../../../../lib/util/httpStatus.js';
2-
import deletePage from '../../services/page/deletePage.js';
2+
import { EvershopRequest } from '../../../../types/request.js';
3+
import { EvershopResponse } from '../../../../types/response.js';
4+
import { deletePage } from '../../services/page/deletePage.js';
35

4-
export default async (request, response, next) => {
6+
export default async (
7+
request: EvershopRequest,
8+
response: EvershopResponse,
9+
next
10+
) => {
511
try {
612
const { id } = request.params;
713
const page = await deletePage(id, {

packages/evershop/src/modules/cms/services/page/createPage.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import {
1111
getValueSync
1212
} from '../../../../lib/util/registry.js';
1313
import { sanitizeRawHtml } from '../../../../lib/util/sanitizeHtml.js';
14+
import type { CmsPageRow, CmsPageDescriptionRow } from '../../../../types/db/index.js';
1415
import { getAjv } from '../../../base/services/getAjv.js';
1516
import pageDataSchema from './pageDataSchema.json' with { type: 'json' };
1617

17-
function validatePageDataBeforeInsert(data) {
18+
function validatePageDataBeforeInsert(data): any {
1819
const ajv = getAjv();
1920
pageDataSchema.required = [
2021
'status',
@@ -33,7 +34,7 @@ function validatePageDataBeforeInsert(data) {
3334
}
3435
}
3536

36-
async function insertPageData(data, connection) {
37+
async function insertPageData(data, connection): Promise<CmsPageRow & CmsPageDescriptionRow> {
3738
const page = await insert('cms_page').given(data).execute(connection);
3839
const description = await insert('cms_page_description')
3940
.given(data)
@@ -51,7 +52,7 @@ async function insertPageData(data, connection) {
5152
* @param {Object} data
5253
* @param {Object} context
5354
*/
54-
async function createPage(data, context) {
55+
const _createPage = async function createPage(data, context): Promise<CmsPageRow & CmsPageDescriptionRow> {
5556
const connection = await getConnection();
5657
await startTransaction(connection);
5758
try {
@@ -76,12 +77,12 @@ async function createPage(data, context) {
7677
}
7778
}
7879

79-
export default async (data, context) => {
80+
export async function createPage(data, context): Promise<CmsPageRow & CmsPageDescriptionRow> {
8081
// Make sure the context is either not provided or is an object
8182
if (context && typeof context !== 'object') {
8283
throw new Error('Context must be an object');
8384
}
84-
const page = await hookable(createPage, context)(data, context);
85+
const page = await hookable(_createPage, context)(data, context);
8586
return page;
8687
};
8788

packages/evershop/src/modules/cms/services/page/deletePage.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@ import {
66
startTransaction
77
} from '@evershop/postgres-query-builder';
88
import { getConnection } from '../../../../lib/postgres/connection.js';
9-
import { hookable, hookBefore, hookAfter } from '../../../../lib/util/hookable.js';
9+
import {
10+
hookable,
11+
hookBefore,
12+
hookAfter
13+
} from '../../../../lib/util/hookable.js';
14+
import type {
15+
CmsPageRow,
16+
CmsPageDescriptionRow
17+
} from '../../../../types/db/index.js';
1018

11-
async function deletePageData(uuid, connection) {
19+
async function deletePageData(uuid, connection): Promise<void> {
1220
await del('cms_page').where('uuid', '=', uuid).execute(connection);
1321
}
1422
/**
1523
* Delete page service. This service will delete a page with all related data
1624
* @param {String} uuid
1725
* @param {Object} context
1826
*/
19-
async function deletePage(uuid, context) {
27+
const _deletePage = async function deletePage(
28+
uuid,
29+
context
30+
): Promise<CmsPageRow & CmsPageDescriptionRow> {
2031
const connection = await getConnection();
2132
await startTransaction(connection);
2233
try {
@@ -43,24 +54,24 @@ async function deletePage(uuid, context) {
4354
await rollback(connection);
4455
throw e;
4556
}
46-
}
57+
};
4758

48-
export default async (uuid, context) => {
59+
export async function deletePage(
60+
uuid,
61+
context
62+
): Promise<CmsPageRow & CmsPageDescriptionRow> {
4963
// Make sure the context is either not provided or is an object
5064
if (context && typeof context !== 'object') {
5165
throw new Error('Context must be an object');
5266
}
53-
const page = await hookable(deletePage, context)(uuid, context);
67+
const page = await hookable(_deletePage, context)(uuid, context);
5468
return page;
55-
};
69+
}
5670

5771
export function hookBeforeDeletePageData(
5872
callback: (
5973
this: Record<string, any>,
60-
...args: [
61-
uuid: any,
62-
connection: any
63-
]
74+
...args: [uuid: any, connection: any]
6475
) => void | Promise<void>,
6576
priority: number = 10
6677
): void {
@@ -70,10 +81,7 @@ export function hookBeforeDeletePageData(
7081
export function hookAfterDeletePageData(
7182
callback: (
7283
this: Record<string, any>,
73-
...args: [
74-
uuid: any,
75-
connection: any
76-
]
84+
...args: [uuid: any, connection: any]
7785
) => void | Promise<void>,
7886
priority: number = 10
7987
): void {
@@ -83,10 +91,7 @@ export function hookAfterDeletePageData(
8391
export function hookBeforeDeletePage(
8492
callback: (
8593
this: Record<string, any>,
86-
...args: [
87-
uuid: any,
88-
context: any
89-
]
94+
...args: [uuid: any, context: any]
9095
) => void | Promise<void>,
9196
priority: number = 10
9297
): void {
@@ -96,10 +101,7 @@ export function hookBeforeDeletePage(
96101
export function hookAfterDeletePage(
97102
callback: (
98103
this: Record<string, any>,
99-
...args: [
100-
uuid: any,
101-
context: any
102-
]
104+
...args: [uuid: any, context: any]
103105
) => void | Promise<void>,
104106
priority: number = 10
105107
): void {

packages/evershop/src/modules/cms/services/page/updatePage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import {
1212
getValueSync
1313
} from '../../../../lib/util/registry.js';
1414
import { sanitizeRawHtml } from '../../../../lib/util/sanitizeHtml.js';
15+
import type { CmsPageRow, CmsPageDescriptionRow } from '../../../../types/db/index.js';
1516
import { getAjv } from '../../../base/services/getAjv.js';
1617
import pageDataSchema from './pageDataSchema.json' with { type: 'json' };
1718

18-
function validatePageDataBeforeInsert(data) {
19+
function validatePageDataBeforeInsert(data): any {
1920
const ajv = getAjv();
2021
pageDataSchema.required = ['status'];
2122
const jsonSchema = getValueSync('updatePageDataJsonSchema', pageDataSchema, {});
@@ -28,7 +29,7 @@ function validatePageDataBeforeInsert(data) {
2829
}
2930
}
3031

31-
async function updatePageData(uuid, data, connection) {
32+
async function updatePageData(uuid, data, connection): Promise<CmsPageRow & CmsPageDescriptionRow> {
3233
const query = select().from('cms_page');
3334
query
3435
.leftJoin('cms_page_description')
@@ -72,7 +73,7 @@ async function updatePageData(uuid, data, connection) {
7273
* @param {Object} data
7374
* @param {Object} context
7475
*/
75-
async function updatePage(uuid, data, context) {
76+
async function updatePage(uuid, data, context): Promise<CmsPageRow & CmsPageDescriptionRow> {
7677
const connection = await getConnection();
7778
await startTransaction(connection);
7879
try {
@@ -99,7 +100,7 @@ async function updatePage(uuid, data, context) {
99100
}
100101
}
101102

102-
export default async (uuid, data, context) => {
103+
export default async (uuid, data, context): Promise<CmsPageRow & CmsPageDescriptionRow> => {
103104
// Make sure the context is either not provided or is an object
104105
if (context && typeof context !== 'object') {
105106
throw new Error('Context must be an object');

packages/evershop/src/modules/customer/services/customer/createCustomer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CustomerRow } from '@evershop/evershop/types/db/index.js';
21
import {
32
commit,
43
insert,
@@ -22,6 +21,7 @@ import {
2221
getValue,
2322
getValueSync
2423
} from '../../../../lib/util/registry.js';
24+
import { CustomerRow } from '../../../../types/db/index.js';
2525
import { getAjv } from '../../../base/services/getAjv.js';
2626
import customerDataSchema from './customerDataSchema.json' with { type: 'json' };
2727

packages/evershop/src/modules/customer/services/customer/deleteCustomer.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
hookAfter
1414
} from '../../../../lib/util/hookable.js';
1515
import type { CustomerRow } from '../../../../types/db/index.js';
16-
import { CustomerData } from './createCustomer.js';
1716

1817
async function deleteCustomerData(
1918
uuid: string,

packages/evershop/src/modules/promotion/services/coupon/createCoupon.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getValue,
1212
getValueSync
1313
} from '../../../../lib/util/registry.js';
14+
import type { CouponRow } from '../../../../types/db/index.js';
1415
import { getAjv } from '../../../base/services/getAjv.js';
1516
import couponDataSchema from './couponDataSchema.json' with { type: 'json' };
1617

@@ -77,7 +78,7 @@ function validateCouponDataBeforeInsert(data: CouponData): CouponData {
7778
}
7879
}
7980

80-
async function insertCouponData(data: CouponData, connection: PoolClient) {
81+
async function insertCouponData(data: CouponData, connection: PoolClient): Promise<CouponRow> {
8182
const coupon = await insert('coupon').given(data).execute(connection);
8283
return coupon;
8384
}
@@ -87,7 +88,7 @@ async function insertCouponData(data: CouponData, connection: PoolClient) {
8788
* @param {CouponData} data
8889
* @param {Record<string, any>} context
8990
*/
90-
async function createCoupon(data: CouponData, context: Record<string, any>) {
91+
async function createCoupon(data: CouponData, context: Record<string, any>): Promise<CouponRow> {
9192
const connection = await getConnection();
9293
await startTransaction(connection);
9394
try {
@@ -108,7 +109,7 @@ async function createCoupon(data: CouponData, context: Record<string, any>) {
108109
}
109110
}
110111

111-
export default async (data: CouponData, context: Record<string, any> = {}) => {
112+
export default async (data: CouponData, context: Record<string, any> = {}): Promise<CouponRow> => {
112113
// Make sure the context is either not provided or is an object
113114
if (context && typeof context !== 'object') {
114115
throw new Error('Context must be an object');

packages/evershop/src/modules/promotion/services/coupon/deleteCoupon.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
hookBefore,
1313
hookAfter
1414
} from '../../../../lib/util/hookable.js';
15+
import type { CouponRow } from '../../../../types/db/index.js';
1516
import type { CouponData } from './createCoupon.js';
1617

1718
async function deleteCouponData(
@@ -26,7 +27,10 @@ async function deleteCouponData(
2627
* @param {string} uuid
2728
* @param {Record<string, any>} context
2829
*/
29-
async function deleteCoupon(uuid: string, context: Record<string, any>) {
30+
async function deleteCoupon(
31+
uuid: string,
32+
context: Record<string, any>
33+
): Promise<CouponRow> {
3034
const connection = await getConnection();
3135
await startTransaction(connection);
3236
try {
@@ -48,7 +52,10 @@ async function deleteCoupon(uuid: string, context: Record<string, any>) {
4852
}
4953
}
5054

51-
export default async (uuid: string, context: Record<string, any> = {}) => {
55+
export default async (
56+
uuid: string,
57+
context: Record<string, any> = {}
58+
): Promise<CouponRow> => {
5259
// Make sure the context is either not provided or is an object
5360
if (context && typeof context !== 'object') {
5461
throw new Error('Context must be an object');

0 commit comments

Comments
 (0)