Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 101 additions & 32 deletions src/access/infra/repositories/AccessRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { ApiConfig, DataverseApiAuthMechanism } from '../../../core/infra/repositories/ApiConfig'
import { WriteError } from '../../../core/domain/repositories/WriteError'
import { ApiConstants } from '../../../core/infra/repositories/ApiConstants'
import { ApiRepository } from '../../../core/infra/repositories/ApiRepository'
import {
buildRequestConfig,
buildRequestUrl
} from '../../../core/infra/repositories/apiConfigBuilders'
import { GuestbookResponseDTO } from '../../domain/dtos/GuestbookResponseDTO'
import { IAccessRepository } from '../../domain/repositories/IAccessRepository'

Expand All @@ -13,14 +20,7 @@
const endpoint = this.buildApiEndpoint(`${this.accessResourceName}/datafile`, undefined, fileId)
const queryParams = format ? { signed: true, format } : { signed: true }

return this.doPost(endpoint, guestbookResponse, queryParams)
.then((response) => {
const signedUrl = response.data.data.signedUrl
return signedUrl
})
.catch((error) => {
throw error
})
return await this.submitGuestbookDownload(endpoint, guestbookResponse, queryParams)
}

public async submitGuestbookForDatafilesDownload(
Expand All @@ -30,21 +30,14 @@
): Promise<string> {
const queryParams = format ? { signed: true, format } : { signed: true }

return this.doPost(
return await this.submitGuestbookDownload(
this.buildApiEndpoint(
this.accessResourceName,
`datafiles/${Array.isArray(fileIds) ? fileIds.join(',') : fileIds}`
),
guestbookResponse,
queryParams
)
.then((response) => {
const signedUrl = response.data.data.signedUrl
return signedUrl
})
.catch((error) => {
throw error
})
}

public async submitGuestbookForDatasetDownload(
Expand All @@ -59,14 +52,7 @@
)
const queryParams = format ? { signed: true, format } : { signed: true }

return this.doPost(endpoint, guestbookResponse, queryParams)
.then((response) => {
const signedUrl = response.data.data.signedUrl
return signedUrl
})
.catch((error) => {
throw error
})
return await this.submitGuestbookDownload(endpoint, guestbookResponse, queryParams)
}

public async submitGuestbookForDatasetVersionDownload(
Expand All @@ -82,13 +68,96 @@
)
const queryParams = format ? { signed: true, format } : { signed: true }

return this.doPost(endpoint, guestbookResponse, queryParams)
.then((response) => {
const signedUrl = response.data.data.signedUrl
return signedUrl
})
.catch((error) => {
throw error
})
return await this.submitGuestbookDownload(endpoint, guestbookResponse, queryParams)
}

private async submitGuestbookDownload(
apiEndpoint: string,
guestbookResponse: GuestbookResponseDTO,
queryParams: object
): Promise<string> {
const requestConfig = buildRequestConfig(
true,
queryParams,
ApiConstants.CONTENT_TYPE_APPLICATION_JSON
)
const response = await fetch(
this.buildUrlWithQueryParams(buildRequestUrl(apiEndpoint), queryParams),
{
method: 'POST',
headers: this.buildFetchHeaders(requestConfig.headers),
credentials: this.getFetchCredentials(requestConfig.withCredentials),
body: JSON.stringify(guestbookResponse)
}
).catch((error) => {
throw new WriteError(error instanceof Error ? error.message : String(error))
})

const responseData = await this.parseResponseBody(response)

if (!response.ok) {
throw new WriteError(this.buildFetchErrorMessage(response.status, responseData))
}

return responseData.data.signedUrl as string
}

private getFetchCredentials(withCredentials?: boolean): RequestCredentials | undefined {
if (ApiConfig.dataverseApiAuthMechanism === DataverseApiAuthMechanism.BEARER_TOKEN) {
return 'omit'
}

if (withCredentials) {
return 'include'
}

return undefined
}

private buildUrlWithQueryParams(requestUrl: string, queryParams: object): string {
const url = new URL(requestUrl)

Object.entries(queryParams).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
url.searchParams.append(key, String(value))
}
})

return url.toString()
}

private buildFetchHeaders(headers?: Record<string, unknown>): Record<string, string> {
const fetchHeaders: Record<string, string> = {}

if (!headers) {
return fetchHeaders
}

Object.entries(headers).forEach(([key, value]) => {
if (value !== undefined) {
fetchHeaders[key] = String(value)
}
})

return fetchHeaders
}

private async parseResponseBody(response: Response): Promise<any> {

Check warning on line 145 in src/access/infra/repositories/AccessRepository.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const contentType = response.headers.get('content-type') ?? ''

if (contentType.includes('application/json')) {
return await response.json()
}

return await response.text()
}

private buildFetchErrorMessage(status: number, responseData: any): string {

Check warning on line 155 in src/access/infra/repositories/AccessRepository.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
const message =
typeof responseData === 'string'
? responseData
: responseData?.message || responseData?.data?.message || 'unknown error'

return `[${status}] ${message}`
}
}
69 changes: 69 additions & 0 deletions test/unit/access/AccessRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* @jest-environment jsdom
*/

import { AccessRepository } from '../../../src/access/infra/repositories/AccessRepository'
import { GuestbookResponseDTO } from '../../../src/access/domain/dtos/GuestbookResponseDTO'
import {
ApiConfig,
DataverseApiAuthMechanism
} from '../../../src/core/infra/repositories/ApiConfig'
import { TestConstants } from '../../testHelpers/TestConstants'

describe('AccessRepository', () => {
const sut = new AccessRepository()
const guestbookResponse: GuestbookResponseDTO = {
guestbookResponse: {
answers: [{ id: 1, value: 'question 1' }]
}
}

beforeEach(() => {
window.localStorage.setItem(
TestConstants.TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY,
JSON.stringify(TestConstants.TEST_DUMMY_BEARER_TOKEN)
)
})

afterEach(() => {
window.localStorage.clear()
})

test('uses fetch with credentials omit for bearer token auth', async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.BEARER_TOKEN,
undefined,
TestConstants.TEST_BEARER_TOKEN_LOCAL_STORAGE_KEY
)

const fetchMock = jest.fn().mockResolvedValue({
ok: true,
status: 200,
headers: new Headers({ 'content-type': 'application/json' }),
json: jest.fn().mockResolvedValue({
data: {
signedUrl: 'https://signed.dataset'
}
})
} as unknown as Response)

global.fetch = fetchMock as typeof fetch

const actual = await sut.submitGuestbookForDatasetDownload(123, guestbookResponse, 'original')

expect(fetchMock).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/access/dataset/123?signed=true&format=original`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${TestConstants.TEST_DUMMY_BEARER_TOKEN}`
},
credentials: 'omit',
body: JSON.stringify(guestbookResponse)
}
)
expect(actual).toBe('https://signed.dataset')
})
})
Loading