FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
astro-api-nodejs-client/src/http.ts at master · astrologyapi/astro-api-nodejs-client · GitHub
astrologyapi
/
astro-api-nodejs-client
Public
Notifications
You must be signed in to change notification settings
Fork
9
Star
9
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
astro-api-nodejs-client
/
src
/
http.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
210 lines (182 loc) · 6.17 KB
Breadcrumbs
astro-api-nodejs-client
/
src
/
http.ts
Copy path
File metadata and controls
210 lines (182 loc) · 6.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import
{
AstrologyAPIError
,
AuthenticationError
,
NetworkError
,
PlanRestrictedError
,
QuotaExceededError
,
RateLimitError
,
ServerError
,
ValidationError
,
}
from
"./errors"
;
import
{
SDK_VERSION
}
from
"./version"
;
const
BASE_JSON_URL
=
"https://json.astrologyapi.com"
;
const
BASE_PDF_URL
=
"https://pdf.astrologyapi.com"
;
export
type
ApiDomain
=
"json"
|
"pdf"
;
export
interface
RequestOptions
{
endpoint
:
string
;
body
?:
Record
<
string
,
unknown
>
;
domain
?:
ApiDomain
;
language
?:
string
|
undefined
;
encoding
?:
"json"
|
"form-urlencoded"
;
}
export
interface
HttpClientConfig
{
userId
?:
string
;
apiKey
:
string
;
version
:
string
;
forceHeaderAuth
?:
boolean
;
}
/**
* Internal HTTP client. Handles auth, base URL selection,
* and maps API error codes to typed SDK errors.
*/
export
class
HttpClient
{
private
readonly
userId
?:
string
;
private
readonly
apiKey
:
string
;
private
readonly
version
:
string
;
private
readonly
useHeaderAuth
:
boolean
;
constructor
(
userId
:
string
,
apiKey
:
string
,
version
:
string
)
;
constructor
(
config
:
HttpClientConfig
)
;
constructor
(
userIdOrConfig
:
string
|
HttpClientConfig
,
apiKey
?:
string
,
version
?:
string
,
)
{
const
resolved
=
typeof
userIdOrConfig
===
"string"
?
{
userId
:
userIdOrConfig
,
apiKey
:
apiKey
??
""
,
version
:
version
??
"v1"
,
}
:
userIdOrConfig
;
this
.
userId
=
resolved
.
userId
;
this
.
apiKey
=
resolved
.
apiKey
;
this
.
version
=
resolved
.
version
;
this
.
useHeaderAuth
=
resolved
.
forceHeaderAuth
??
shouldUseHeaderAuth
(
resolved
.
apiKey
)
;
}
async
post
<
T
=
unknown
>
(
options
:
RequestOptions
)
:
Promise
<
T
>
{
const
{
endpoint
,
body
=
{
}
,
domain
=
"json"
,
language
,
encoding
=
"json"
}
=
options
;
const
baseUrl
=
domain
===
"pdf"
?
BASE_PDF_URL
:
BASE_JSON_URL
;
const
url
=
`
${
baseUrl
}
/
${
this
.
version
}
/
${
endpoint
}
`
;
const
headers
:
Record
<
string
,
string
>
=
{
"Content-Type"
:
encoding
===
"form-urlencoded"
?
"application/x-www-form-urlencoded"
:
"application/json"
,
"User-Agent"
:
`astrologyapi-node/
${
SDK_VERSION
}
`
,
}
;
if
(
this
.
useHeaderAuth
)
{
headers
[
"x-astrologyapi-key"
]
=
this
.
apiKey
;
}
else
{
if
(
!
this
.
userId
)
{
throw
new
Error
(
"HttpClient: userId is required when using Basic Authorization."
)
;
}
headers
[
"Authorization"
]
=
`Basic
${
Buffer
.
from
(
`
${
this
.
userId
}
:
${
this
.
apiKey
}
`
)
.
toString
(
"base64"
)
}
`
;
}
if
(
language
)
{
headers
[
"Accept-Language"
]
=
language
;
}
let
response
:
Response
;
try
{
const
requestBody
=
encoding
===
"form-urlencoded"
?
buildFormEncodedBody
(
body
)
:
JSON
.
stringify
(
body
)
;
response
=
await
fetch
(
url
,
{
method
:
"POST"
,
headers
,
body
:
requestBody
,
}
)
;
}
catch
(
cause
)
{
throw
new
NetworkError
(
`Network request failed for
${
endpoint
}
. Check your internet connection.`
,
cause
,
)
;
}
// Parse body regardless of status so we can include it in errors
let
data
:
unknown
;
const
contentType
=
response
.
headers
.
get
(
"content-type"
)
??
""
;
try
{
if
(
contentType
.
includes
(
"application/json"
)
)
{
data
=
await
response
.
json
(
)
;
}
else
if
(
contentType
.
includes
(
"application/pdf"
)
||
domain
===
"pdf"
)
{
data
=
await
response
.
arrayBuffer
(
)
;
}
else
{
data
=
await
response
.
text
(
)
;
}
}
catch
{
data
=
null
;
}
if
(
response
.
ok
)
{
return
data
as
T
;
}
// Map HTTP status → typed error
const
message
=
extractMessage
(
data
)
??
response
.
statusText
;
switch
(
response
.
status
)
{
case
400
:
case
422
:
throw
new
ValidationError
(
message
,
undefined
,
data
)
;
case
401
:
throw
new
AuthenticationError
(
message
||
"Authentication failed. Check your credentials."
,
401
,
data
,
)
;
case
402
:
throw
new
QuotaExceededError
(
message
||
"API quota exceeded. Please upgrade your plan at astrologyapi.com."
,
data
,
)
;
case
403
:
throw
new
PlanRestrictedError
(
message
||
"This endpoint is not available on your current plan."
,
data
,
)
;
case
429
:
{
const
retryAfter
=
parseRetryAfter
(
response
)
;
throw
new
RateLimitError
(
message
||
"Rate limit exceeded. Please wait before retrying."
,
retryAfter
,
data
,
)
;
}
default
:
if
(
response
.
status
>=
500
)
{
throw
new
ServerError
(
message
||
`AstrologyAPI server error (
${
response
.
status
}
). Please try again later.`
,
response
.
status
,
data
,
)
;
}
throw
new
AstrologyAPIError
(
message
,
response
.
status
,
data
)
;
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────
function
extractMessage
(
data
:
unknown
)
:
string
|
undefined
{
if
(
typeof
data
===
"string"
)
return
data
;
if
(
typeof
data
===
"object"
&&
data
!==
null
)
{
const
obj
=
data
as
Record
<
string
,
unknown
>
;
const
candidate
=
obj
[
"message"
]
??
obj
[
"error"
]
??
obj
[
"msg"
]
;
if
(
typeof
candidate
===
"string"
)
return
candidate
;
}
return
undefined
;
}
function
parseRetryAfter
(
response
:
Response
)
:
number
|
undefined
{
const
header
=
response
.
headers
.
get
(
"retry-after"
)
;
if
(
header
===
null
)
return
undefined
;
const
seconds
=
parseInt
(
header
,
10
)
;
return
isNaN
(
seconds
)
?
undefined
:
seconds
;
}
function
shouldUseHeaderAuth
(
apiKey
:
string
)
:
boolean
{
return
apiKey
.
includes
(
"ak-"
)
;
}
function
buildFormEncodedBody
(
body
:
Record
<
string
,
unknown
>
)
:
URLSearchParams
{
const
params
=
new
URLSearchParams
(
)
;
for
(
const
[
key
,
value
]
of
Object
.
entries
(
body
)
)
{
if
(
value
===
undefined
||
value
===
null
)
{
continue
;
}
params
.
set
(
key
,
String
(
value
)
)
;
}
return
params
;
}
Back
|
FazBrowse Home
|
New Git URL