@paakd/api 0.0.1
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/dist/src/index.js +21 -0
- package/package.json +59 -0
- package/src/address.spec.ts +662 -0
- package/src/address.ts +300 -0
- package/src/auth.spec.ts +771 -0
- package/src/auth.ts +168 -0
- package/src/compressor/brotli.ts +26 -0
- package/src/index.ts +5 -0
- package/src/interceptors.spec.ts +1343 -0
- package/src/interceptors.ts +224 -0
- package/src/policies.spec.ts +595 -0
- package/src/policies.ts +431 -0
- package/src/products.spec.ts +710 -0
- package/src/products.ts +112 -0
- package/src/profile.spec.ts +626 -0
- package/src/profile.ts +169 -0
- package/src/proto/auth/v1/entities/auth.proto +140 -0
- package/src/proto/auth/v1/entities/policy.proto +57 -0
- package/src/proto/auth/v1/service.proto +26 -0
- package/src/proto/customers/v1/entities/address.proto +101 -0
- package/src/proto/customers/v1/entities/profile.proto +118 -0
- package/src/proto/customers/v1/service.proto +36 -0
- package/src/proto/files/v1/entities/file.proto +62 -0
- package/src/proto/files/v1/service.proto +19 -0
- package/src/proto/products/v1/entities/category.proto +98 -0
- package/src/proto/products/v1/entities/collection.proto +72 -0
- package/src/proto/products/v1/entities/product/create.proto +41 -0
- package/src/proto/products/v1/entities/product/option.proto +17 -0
- package/src/proto/products/v1/entities/product/shared.proto +255 -0
- package/src/proto/products/v1/entities/product/update.proto +66 -0
- package/src/proto/products/v1/entities/tag.proto +73 -0
- package/src/proto/products/v1/entities/taxonomy.proto +146 -0
- package/src/proto/products/v1/entities/type.proto +98 -0
- package/src/proto/products/v1/entities/variant.proto +127 -0
- package/src/proto/products/v1/service.proto +78 -0
- package/src/proto/promotions/v1/entities/campaign.proto +145 -0
- package/src/proto/promotions/v1/service.proto +17 -0
- package/src/proto/stocknodes/v1/entities/stocknode.proto +167 -0
- package/src/proto/stocknodes/v1/service.proto +21 -0
- package/src/registration.ts +170 -0
- package/src/test-utils.ts +176 -0
package/src/auth.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { Code, ConnectError, createClient } from '@connectrpc/connect'
|
|
2
|
+
import { createGrpcTransport } from '@connectrpc/connect-node'
|
|
3
|
+
import { getCheckoutConfig } from '@paakd/config'
|
|
4
|
+
import { AuthService } from '../gen/src/proto/auth/v1/service_pb'
|
|
5
|
+
import type { ChangePasswordRequest } from '../gen/src/proto/customers/v1/entities/profile_pb'
|
|
6
|
+
import { CustomerService } from '../gen/src/proto/customers/v1/service_pb'
|
|
7
|
+
import type { LoginResponse } from '../gen/src/proto/auth/v1/entities/auth_pb'
|
|
8
|
+
import { brotliCompression } from './compressor/brotli'
|
|
9
|
+
import {
|
|
10
|
+
createAuthenticationInterceptor,
|
|
11
|
+
createCustomerAuthenticationInterceptor,
|
|
12
|
+
createHeadersInterceptor,
|
|
13
|
+
} from './interceptors'
|
|
14
|
+
import type { ApiResponse, SuccessResponse, ErrorResponse } from './address'
|
|
15
|
+
|
|
16
|
+
export interface LoginRequestProps {
|
|
17
|
+
body: {
|
|
18
|
+
email: string
|
|
19
|
+
password: string
|
|
20
|
+
}
|
|
21
|
+
headers: Record<string, string | null>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RefreshTokenRequestProps {
|
|
25
|
+
body: {
|
|
26
|
+
refreshToken: string
|
|
27
|
+
}
|
|
28
|
+
headers: Record<string, string | null>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ChangePasswordRequestProps {
|
|
32
|
+
body: ChangePasswordRequest & { jwt: string }
|
|
33
|
+
headers: Record<string, string | null>
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Re-export response types for auth
|
|
37
|
+
export type { ApiResponse, SuccessResponse, ErrorResponse }
|
|
38
|
+
|
|
39
|
+
export async function login({
|
|
40
|
+
body: { email, password },
|
|
41
|
+
headers,
|
|
42
|
+
}: LoginRequestProps): Promise<ApiResponse<LoginResponse>> {
|
|
43
|
+
const checkoutConfig = await getCheckoutConfig()
|
|
44
|
+
const transport = createGrpcTransport({
|
|
45
|
+
baseUrl: checkoutConfig.enterpriseURL,
|
|
46
|
+
interceptors: [
|
|
47
|
+
createHeadersInterceptor(headers),
|
|
48
|
+
createAuthenticationInterceptor(checkoutConfig),
|
|
49
|
+
],
|
|
50
|
+
acceptCompression: [brotliCompression],
|
|
51
|
+
sendCompression: brotliCompression,
|
|
52
|
+
})
|
|
53
|
+
const auth = createClient(AuthService, transport)
|
|
54
|
+
try {
|
|
55
|
+
const value = await auth.login({
|
|
56
|
+
email,
|
|
57
|
+
password,
|
|
58
|
+
})
|
|
59
|
+
console.log('Login successful for email:', email)
|
|
60
|
+
return {
|
|
61
|
+
value,
|
|
62
|
+
status: 'success',
|
|
63
|
+
}
|
|
64
|
+
} catch (err: unknown) {
|
|
65
|
+
console.log('Error during login:', err)
|
|
66
|
+
if (err instanceof ConnectError) {
|
|
67
|
+
return {
|
|
68
|
+
code: err.code,
|
|
69
|
+
rawMessage: err.rawMessage,
|
|
70
|
+
message: err.rawMessage,
|
|
71
|
+
status: 'failed',
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
code: Code.Internal,
|
|
77
|
+
rawMessage: 'An unexpected error occurred during login.',
|
|
78
|
+
message: 'An unexpected error occurred during login.',
|
|
79
|
+
status: 'failed',
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function refreshToken({
|
|
85
|
+
body: { refreshToken },
|
|
86
|
+
headers,
|
|
87
|
+
}: RefreshTokenRequestProps): Promise<ApiResponse<LoginResponse>> {
|
|
88
|
+
const checkoutConfig = await getCheckoutConfig()
|
|
89
|
+
const transport = createGrpcTransport({
|
|
90
|
+
baseUrl: checkoutConfig.enterpriseURL,
|
|
91
|
+
interceptors: [
|
|
92
|
+
createHeadersInterceptor(headers),
|
|
93
|
+
createAuthenticationInterceptor(checkoutConfig),
|
|
94
|
+
],
|
|
95
|
+
acceptCompression: [brotliCompression],
|
|
96
|
+
sendCompression: brotliCompression,
|
|
97
|
+
})
|
|
98
|
+
const auth = createClient(AuthService, transport)
|
|
99
|
+
try {
|
|
100
|
+
const value = await auth.refreshToken({
|
|
101
|
+
refreshToken,
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
value,
|
|
106
|
+
status: 'success',
|
|
107
|
+
}
|
|
108
|
+
} catch (err: unknown) {
|
|
109
|
+
if (err instanceof ConnectError) {
|
|
110
|
+
return {
|
|
111
|
+
code: err.code,
|
|
112
|
+
rawMessage: err.rawMessage,
|
|
113
|
+
message: err.rawMessage,
|
|
114
|
+
status: 'failed',
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
code: Code.Internal,
|
|
120
|
+
rawMessage: 'An unexpected error occurred while refreshing the token.',
|
|
121
|
+
message: 'An unexpected error occurred while refreshing the token.',
|
|
122
|
+
status: 'failed',
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function changePassword({
|
|
128
|
+
body: { jwt, ...prop },
|
|
129
|
+
headers,
|
|
130
|
+
}: ChangePasswordRequestProps): Promise<ApiResponse<any>> {
|
|
131
|
+
const checkoutConfig = await getCheckoutConfig()
|
|
132
|
+
const transport = createGrpcTransport({
|
|
133
|
+
baseUrl: checkoutConfig.enterpriseURL,
|
|
134
|
+
interceptors: [
|
|
135
|
+
createHeadersInterceptor(headers),
|
|
136
|
+
createAuthenticationInterceptor(checkoutConfig),
|
|
137
|
+
createCustomerAuthenticationInterceptor(jwt),
|
|
138
|
+
],
|
|
139
|
+
acceptCompression: [brotliCompression],
|
|
140
|
+
sendCompression: brotliCompression,
|
|
141
|
+
})
|
|
142
|
+
const profile = createClient(CustomerService, transport)
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const value = await profile.changePassword(prop)
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
value,
|
|
149
|
+
status: 'success',
|
|
150
|
+
}
|
|
151
|
+
} catch (err: unknown) {
|
|
152
|
+
if (err instanceof ConnectError) {
|
|
153
|
+
return {
|
|
154
|
+
code: err.code,
|
|
155
|
+
rawMessage: err.rawMessage,
|
|
156
|
+
message: err.rawMessage,
|
|
157
|
+
status: 'failed',
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
code: Code.Internal,
|
|
163
|
+
rawMessage: 'profile change password failed',
|
|
164
|
+
message: 'An unexpected error occurred during profile change password.',
|
|
165
|
+
status: 'failed',
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Compression } from '@connectrpc/connect/protocol'
|
|
2
|
+
|
|
3
|
+
const brotli = require('brotli')
|
|
4
|
+
|
|
5
|
+
export const brotliCompression: Compression = {
|
|
6
|
+
name: 'br',
|
|
7
|
+
compress: async (bytes: Uint8Array): Promise<Uint8Array<ArrayBuffer>> => {
|
|
8
|
+
return new Uint8Array(
|
|
9
|
+
brotli.compress(bytes, {
|
|
10
|
+
mode: 0, // 0 = generic, 1 = text, 2 = font (WOFF2)
|
|
11
|
+
quality: 9, // 0 - 11
|
|
12
|
+
lgwin: 22, // window size
|
|
13
|
+
})
|
|
14
|
+
)
|
|
15
|
+
},
|
|
16
|
+
decompress: async (
|
|
17
|
+
bytes: Uint8Array,
|
|
18
|
+
readMaxBytes: number
|
|
19
|
+
): Promise<Uint8Array<ArrayBuffer>> => {
|
|
20
|
+
const decompressed = brotli.decompress(bytes)
|
|
21
|
+
if (decompressed.length > readMaxBytes) {
|
|
22
|
+
throw new Error('Decompressed size exceeds readMaxBytes limit')
|
|
23
|
+
}
|
|
24
|
+
return new Uint8Array(decompressed)
|
|
25
|
+
},
|
|
26
|
+
}
|