Skip to content

Commit

Permalink
Merge pull request #8 from getfursure/fix/user-intent-create
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbrannan authored Oct 3, 2022
2 parents 7ec8644 + d50540b commit 83a4766
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

Updates are from recent to latest (most recent is immediately below)

## 1.0.0-alpha.11 ~ 2022-10-03

### Major Change
- `AuthResource` has `basicAuth` getter for setting the Basic Auth Credentials on some of the resource requests.
- User and Intent resources now receive Auth resource

### Fixed
- Astra User Intent Create sends `auth` using client and secret. Previously, this caused `POST` `user_intent` to fail with Authorization error.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"version": "1.0.0-alpha.10",
"version": "1.0.0-alpha.11",
"description": "Astra Finance JS SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/client/astra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class Astra {

protected _initResources() {
this.auth = new AuthResource(this._client, this._clientId, this._clientSecret)
this.users = new Users(this._client)
this.users = new Users(this._client, this.auth)
this.routines = new RoutinesResource(this._client, this.auth)
this.cards = new CardsResource(this._client, this.auth)
this.transfers = new TransfersResource(this._client, this.auth)
Expand Down
14 changes: 9 additions & 5 deletions src/resources/auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Axios } from 'axios'
import { Axios, AxiosBasicCredentials } from 'axios'
import { AstraResponse } from '../../lib/AstraResponse'
import { AstraResponseError } from '../../lib/AstraResponseError'

Expand Down Expand Up @@ -30,6 +30,13 @@ export class AuthResource {
this._clientSecret = clientSecret
}

get basicAuth(): AxiosBasicCredentials {
return {
username: this._clientId,
password: this._clientSecret,
}
}

async createAccessToken(request: AstraCreateAccessTokenRequest): Promise<AstraResponse<AstraAccessTokenResponse>> {
const headers: { [key: string]: string } = {
'Content-Type': 'application/x-www-form-urlencoded',
Expand All @@ -40,10 +47,7 @@ export class AuthResource {
`grant_type=authorization_code&code=${request.code}&redirect_uri=${request.redirect_uri}`,
{
headers,
auth: {
username: this._clientId,
password: this._clientSecret,
},
auth: this.basicAuth,
}
)

Expand Down
10 changes: 5 additions & 5 deletions src/resources/users/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Axios } from 'axios'
import { Intents } from './intents'
import { AuthResource } from '../auth'

export class Users {
private _client: Axios

intents: Intents
constructor(client: Axios) {
this._client = client
this.intents = new Intents(this._client)
auth: AuthResource

constructor(client: Axios, auth: AuthResource) {
this.intents = new Intents(client, this.auth)
}
}
3 changes: 2 additions & 1 deletion src/resources/users/intents.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Intents } from './intents'
import axios from 'axios'
import { AuthResource } from '../auth'

describe('Users > intents', () => {
jest.mock('axios')
const mockAxios = axios as jest.Mocked<typeof axios>

describe('create', () => {
it('throws if a required request parameter is missing', async () => {
const intents = new Intents(mockAxios)
const intents = new Intents(mockAxios, new AuthResource(mockAxios, '1', '2'))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const create = () => intents.create({ email: 'test@test.com' } as any)
await expect(create()).rejects.toThrowError(
Expand Down
24 changes: 14 additions & 10 deletions src/resources/users/intents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { omit } from 'lodash'
import transformRequest from '../../lib/utils/transformRequest'
import { AstraResponse } from '../../lib/AstraResponse'
import createUserIntentValidator from './validators/createUserIntentValidator'
import { AuthResource } from '../auth'

export interface UserIntentRequest {
email: string
Expand All @@ -27,21 +28,22 @@ export interface CreateUserIntentResponse {
}

export class Intents {
private _path = 'v1/user_intent'
protected _path = 'v1/user_intent'
protected _client: Axios
protected _authResource: AuthResource

private _client: Axios
constructor(client: Axios) {
constructor(client: Axios, auth: AuthResource) {
this._client = client
this._authResource = auth
}

async create(request: UserIntentRequest): Promise<AstraResponse<CreateUserIntentResponse>> {
try {
const { validate, ajv } = createUserIntentValidator()

if (!validate(request)) {
throw new Error(ajv.errorsText(validate.errors))
}
const { validate, ajv } = createUserIntentValidator()

if (!validate(request)) {
throw new Error(ajv.errorsText(validate.errors))
}
try {
// This request to astra has mixed casing, both snake and camel as seen in the omitted fields below
const transformedRequest = {
...transformRequest(omit(request, ['address1', 'address2'])),
Expand All @@ -53,7 +55,9 @@ export class Intents {
CreateUserIntentResponse,
AxiosResponse<CreateUserIntentResponse>,
UserIntentRequest
>(this._path, transformedRequest)
>(this._path, transformedRequest, {
auth: this._authResource.basicAuth,
})

if (axios.isAxiosError(data)) {
const responseError = data as AxiosError
Expand Down

0 comments on commit 83a4766

Please sign in to comment.