1
- import axios , { AxiosError } from "axios"
1
+ import axios , { AxiosError , AxiosInstance } from "axios"
2
+
3
+ axios . defaults . baseURL = process . env . CLIENT_SERVER || "http://localhost:9000"
4
+ let region = undefined as any
2
5
3
6
export async function seedData ( ) {
7
+ const axios = getOrInitAxios ( )
4
8
return {
5
9
user : await seedUser ( ) ,
6
10
}
7
11
}
8
12
9
- function getUrl ( path : string ) {
10
- const baseUrl = process . env . CLIENT_SERVER || "http://localhost:9000"
11
- const url = new URL ( path , baseUrl )
12
- return url . toString ( )
13
- }
14
-
15
- async function seedUser ( ) {
13
+ export async function seedUser ( email ?: string , password ?: string ) {
16
14
const user = {
17
15
first_name : "Test" ,
18
16
last_name : "User" ,
19
-
20
- password : "password" ,
17
+ email :
email || "[email protected] " ,
18
+ password : password || "password" ,
21
19
}
22
20
try {
23
- await axios . post ( getUrl ( "/store/customers" ) , user )
21
+ await axios . post ( "/store/customers" , user )
24
22
return user
25
23
} catch ( e : unknown ) {
26
- e = e as AxiosError
27
24
if ( e instanceof AxiosError ) {
28
25
if ( e . response && e . response . status ) {
29
26
const status = e . response . status
@@ -36,3 +33,70 @@ async function seedUser() {
36
33
}
37
34
}
38
35
}
36
+
37
+ async function loadRegion ( axios : AxiosInstance ) {
38
+ const resp = await axios . get ( "/admin/regions" )
39
+ region = resp . data . regions . filter ( ( r : any ) => r . currency_code === "usd" ) [ 0 ]
40
+ }
41
+
42
+ async function getOrInitAxios ( axios ?: AxiosInstance ) {
43
+ if ( ! axios ) {
44
+ axios = await loginAdmin ( )
45
+ }
46
+ if ( ! region ) {
47
+ await loadRegion ( axios )
48
+ }
49
+ return axios
50
+ }
51
+
52
+ export async function seedGiftcard ( axios ?: AxiosInstance ) {
53
+ axios = await getOrInitAxios ( axios )
54
+ const resp = await axios . post ( "/admin/gift-cards" , {
55
+ region_id : region . id ,
56
+ value : 10000 ,
57
+ } )
58
+ resp . data . gift_card . amount = resp . data . gift_card . value . toString ( )
59
+ return resp . data . gift_card as {
60
+ id : string
61
+ code : string
62
+ value : number
63
+ amount : string
64
+ balance : string
65
+ }
66
+ }
67
+
68
+ export async function seedDiscount ( axios ?: AxiosInstance ) {
69
+ axios = await getOrInitAxios ( axios )
70
+ const amount = 2000
71
+ const resp = await axios . post ( "/admin/discounts" , {
72
+ code : "TEST_DISCOUNT_FIXED" ,
73
+ regions : [ region . id ] ,
74
+ rule : {
75
+ type : "fixed" ,
76
+ value : amount ,
77
+ allocation : "total" ,
78
+ } ,
79
+ } )
80
+ const discount = resp . data . discount
81
+ return {
82
+ id : discount . id ,
83
+ code : discount . code ,
84
+ rule_id : discount . rule_id ,
85
+ amount,
86
+ }
87
+ }
88
+
89
+ async function loginAdmin ( ) {
90
+ const resp = await axios . post ( "/admin/auth/token" , {
91
+ email :
process . env . MEDUSA_ADMIN_EMAIL || "[email protected] " ,
92
+ password : process . env . MEDUSA_ADMIN_PASSWORD || "supersecret" ,
93
+ } )
94
+ if ( resp . status !== 200 ) {
95
+ throw { error : "must be able to log in user" }
96
+ }
97
+ return axios . create ( {
98
+ headers : {
99
+ Authorization : `Bearer ${ resp . data . access_token } ` ,
100
+ } ,
101
+ } )
102
+ }
0 commit comments