Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NON-270: Improve REST client #238

Merged
merged 3 commits into from
Oct 4, 2023
Merged

NON-270: Improve REST client #238

merged 3 commits into from
Oct 4, 2023

Conversation

ushkarev
Copy link
Contributor

@ushkarev ushkarev commented Oct 3, 2023

REST client improvements:

  • Adds commonly used PATCH, PUT and DELETE methods
  • Allows passing query parameters to all methods (so you don't need to build a complex url path remembering to encodeURIComponent)
  • Makes response body type generic (was always unknown)

Also, propagate user types into res.locals in request handlers.

Before:

 PASS  server/data/restClient.test.ts
  POST
    ✓ Should return response body
    ✓ Should return raw response body
    ✓ Should not retry by default
    ✓ retries if configured to do so
    ✓ can recover through retries

After:

 PASS  server/data/restClient.test.ts
  Method: get
    ✓ should return response body
    ✓ should return raw response body
    ✓ should retry by default
    ✓ can recover through retries
  Method: patch
    ✓ should return response body
    ✓ should return raw response body
    ✓ should not retry by default
    ✓ should retry if configured to do so
    ✓ can recover through retries
  Method: post
    ✓ should return response body
    ✓ should return raw response body
    ✓ should not retry by default
    ✓ should retry if configured to do so
    ✓ can recover through retries
  Method: put
    ✓ should return response body
    ✓ should return raw response body
    ✓ should not retry by default
    ✓ should retry if configured to do so
    ✓ can recover through retries
  Method: delete
    ✓ should return response body
    ✓ should return raw response body
    ✓ should retry by default
    ✓ can recover through retries

Comment on lines +36 to +43
username: string
name?: string
active?: boolean
authSource?: string
uuid?: string
userId?: string
staffId?: number // deprecated, use userId
activeCaseLoadId?: string // deprecated, use user roles api
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@andrewrlee andrewrlee Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slightly odd that some of these fields are prison specific - though activeCaseLoadId was here before anyway

Copy link
Contributor

@andrewrlee andrewrlee Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for now as it matches the auth response. I guess auth ideally would expose this as a union type

Copy link
Contributor Author

@ushkarev ushkarev Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it took me quite a while to unpick what user-type info comes from where (the stuff in non-associations is more complex as it also loads caseloads) and this is the simplest thing i felt was meaningful… it's what hmpps-auth (allegedly) returns. in practice, i dunno whether all "auth sources" provide some of these fields. it felt like a sensible thing to mark the nullable ones in hmpps-auth as possibly-undefined here. the link to the UserDetail object above suggests getting caseload stuff from other apis.

still, whether this set of fields is good or good enough, i think it's nice to wire it up into res.locals – right?

auth ideally would expose this as a union type

<3

Comment on lines +26 to +28
interface Locals {
user: Express.User
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setUpCurrentUser middleware always populates res.locals.user via the user service

Mjwillis
Mjwillis previously approved these changes Oct 4, 2023
Copy link
Contributor

@Mjwillis Mjwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

query?: string
interface Request {
path: string
query?: object | string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

think this will need to be Record<string, unknown> otherwise you won't be able to pass anything other than empty object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Record<string, unknown> "satisfies" object. i just used the official type accepted by the superagent function:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8139a88dd28ea580afedc673c18ceb24df95effb/types/superagent/index.d.ts#L166

Copy link
Contributor

@psoleckimoj psoleckimoj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ushkarev ushkarev merged commit 1d8b294 into main Oct 4, 2023
@ushkarev ushkarev deleted the rest-client branch October 4, 2023 11:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants