Got: Storing request specific context in Got

Created on 1 Mar 2019  Â·  9Comments  Â·  Source: sindresorhus/got


Issuehunt badges

What would you like to discuss?

I would like to instrument a Got instance using beforeRequest, afterResponse and beforeError hooks. The plan is to create a new request context for each request in the beforeRequest hook and then access that context in the afterResponse and beforeError hooks to report the request's success/error status.

The natural way to do this would be to just mutate the options object in beforeRequest and set a new property containing the context, this is easy to access in both afterResponse and beforeError through the gotOptions given. My only worry is that by introducing new properties in the options there might be incompatibilities with other hooks or later versions of Got itself.

Are there any guidelines or rules of how to name custom variables inside options? Or alternatively is there any better place where I could store my custom property? I plan to release my instrumentation as a public library, so I just want to confirm that I'm not doing anything wrong here.

Checklist

  • [x] I have read the documentation.




IssueHunt Summary

#
#
#
#

[
<
i
m
g

s
r

c

'
h
t
t
p
s
:
/
/
a
v
a
t
a
r
s
0
.
g
i
t
h
u
b
u
s
e
r
c
o
n
t
e
n
t
.
c
o
m
/
u
/
3
6
8
9
4
7
0
0
?

v

4
'

a
l

t

'
s
z
m
a
r
c
z
a
k
'

w
i
d
t

h

2
4

h
e
i
g
h

t

2
4
>

s
z
m
a
r
c
z
a
k
]
(
h
t
t
p
s
:
/
/
i
s
s
u
e
h
u
n
t
.
i
o
/
u
/
s
z
m
a
r
c
z
a
k
)

h
a
s

b
e
e
n

r
e
w
a
r
d
e
d
.

Backers (Total: $60.00)

- #777 Provide userData option

Tips

IssueHunt has been backed by the following sponsors. Become a sponsor


Rewarded on Issuehunt enhancement good for beginner ✭ help wanted ✭

Most helpful comment

We should document this.

All 9 comments

~You can use Symbols.~

It won't work because the options are cloned. We need to discuss possible solutions.

I think the best would be to use a prefix. Like:

options._myAppData = context;

We should document this.

Thank you for the quick response, I will go with the underscore prefix.

@szmarczak How about we just support a custom option where users can put whatever they want? userData?

@sindresorhus Sounds good! Also it shouldn't be enumerable by default I think...

Agreed

@issuehunt has funded $60.00 to this issue.


@sindresorhus has rewarded $54.00 to @szmarczak. See it on IssueHunt

  • :moneybag: Total deposit: $60.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $6.00
Was this page helpful?
0 / 5 - 0 ratings

Related issues

khizarsonu picture khizarsonu  Â·  3Comments

erfanium picture erfanium  Â·  3Comments

sindresorhus picture sindresorhus  Â·  3Comments

jamestalmage picture jamestalmage  Â·  3Comments

lukehorvat picture lukehorvat  Â·  3Comments