Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/dhis2-verify-commits.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
node-version: 20.x
cache: 'yarn'
- run: yarn add -W @commitlint/config-conventional
- run: yarn add -W @commitlint/config-conventional@13
- id: commitlint
run: |
curl https://raw.githubusercontent.com/dhis2/cli-style/refs/heads/master/config/commitlint.config.js -o commitlint.config.js
Expand Down
5 changes: 2 additions & 3 deletions engine/src/links/RestAPILink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ jest.mock('./RestAPILink/fetchData', () => ({

describe('RestAPILink', () => {
it('should call fetch with the expected URL', async () => {
const link = new RestAPILink({ baseUrl: 'http://url', apiVersion: 42 })
const link = new RestAPILink({ baseUrl: 'http://url' })
await link.executeResourceQuery('read', { resource: 'something' }, {})
expect(fetchData).toHaveBeenCalledWith(
'http://url/api/42/something',
'http://url/api/something',
{
method: 'GET',
},
expect.objectContaining({
config: {
baseUrl: 'http://url',
apiVersion: 42,
},
queryAliasCache: expect.anything(),
})
Expand Down
6 changes: 2 additions & 4 deletions engine/src/links/RestAPILink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { queryToResourcePath } from './RestAPILink/queryToResourcePath'

export class RestAPILink implements DataEngineLink {
public readonly config: DataEngineConfig
public readonly versionedApiPath: string
public readonly unversionedApiPath: string
public readonly apiPath: string

public readonly queryAliasCache: QueryAliasCache = new LRUCache<
string,
Expand All @@ -25,8 +24,7 @@ export class RestAPILink implements DataEngineLink {

public constructor(config: DataEngineConfig) {
this.config = config
this.versionedApiPath = joinPath('api', String(config.apiVersion))
this.unversionedApiPath = joinPath('api')
this.apiPath = joinPath('api')
}

private fetch(path: string, options: RequestInit): Promise<JsonValue> {
Expand Down
4 changes: 1 addition & 3 deletions engine/src/links/RestAPILink/fetchData.aliasing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ const makeResponse = ({

describe('fetchData - query alias creation and caching', () => {
const baseUrl = 'http://example.com'
const apiVersion = 34
const longUrl = `${baseUrl}/very/long/query?with=params&that=make&the=uri&long`
const aliasEndpoint = `${baseUrl}/api/${apiVersion}/query/alias`
const aliasEndpoint = `${baseUrl}/api/query/alias`

let refs: FetchRefs
let aliasCreateCalls = 0
Expand All @@ -50,7 +49,6 @@ describe('fetchData - query alias creation and caching', () => {
queryAliasCache: new LRUCache<string, any>(10),
config: {
baseUrl,
apiVersion,
apiToken: undefined,
},
}
Expand Down
1 change: 0 additions & 1 deletion engine/src/links/RestAPILink/fetchData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ describe('networkFetch', () => {
const mockRefs = {
config: {
baseUrl: '',
apiVersion: 42,
} as DataEngineConfig,
queryAliasCache: new LRUCache<string, QueryAlias>(100),
}
Expand Down
7 changes: 1 addition & 6 deletions engine/src/links/RestAPILink/fetchData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ const createQueryAlias = async (
refs: FetchDataRefs
) => {
const alias = <QueryAlias>await fetchData(
joinPath(
refs.config.baseUrl,
'api',
String(refs.config.apiVersion),
'query/alias'
),
joinPath(refs.config.baseUrl, 'api', 'query/alias'),
{
signal: requestOptions.signal,
body: JSON.stringify({ target: url }),
Expand Down
29 changes: 5 additions & 24 deletions engine/src/links/RestAPILink/queryToResourcePath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { queryToResourcePath } from './queryToResourcePath'
const createLink = (config: DataEngineConfig) => new RestAPILink(config)
const defaultConfig: DataEngineConfig = {
basePath: '<base>',
apiVersion: '37',
serverVersion: {
major: 2,
minor: 37,
Expand All @@ -16,7 +15,7 @@ const defaultConfig: DataEngineConfig = {
} as unknown as DataEngineConfig

const link = createLink(defaultConfig)
const apiPath = link.versionedApiPath
const apiPath = link.apiPath

const actionPrefix = `dhis-web-commons/`
const actionPostfix = '.action'
Expand Down Expand Up @@ -170,39 +169,21 @@ describe('queryToResourcePath', () => {
expect(() => queryToResourcePath(link, query, 'read')).toThrow()
})

it('should return an unversioned endpoint for the new tracker importer (in version 2.37)', () => {
it('should return an endpoint for the tracker resource', () => {
const query: ResolvedResourceQuery = {
resource: 'tracker',
}
expect(queryToResourcePath(link, query, 'read')).toBe(
`${link.unversionedApiPath}/tracker`
`${apiPath}/tracker`
)
})

it('should return an unversioned endpoint sub-resources of the new tracker importer (in version 2.37)', () => {
it('should return an unversioned endpoint for the tracker importer', () => {
const query: ResolvedResourceQuery = {
resource: 'tracker/test',
}
expect(queryToResourcePath(link, query, 'read')).toBe(
`${link.unversionedApiPath}/tracker/test`
)
})

it('should return a VERSIONED endpoint for the new tracker importer (in version 2.38)', () => {
const query: ResolvedResourceQuery = {
resource: 'tracker',
}
const v38config: DataEngineConfig = {
...defaultConfig,
serverVersion: {
major: 2,
minor: 38,
patch: 0,
full: '2.38.0',
},
} as DataEngineConfig
expect(queryToResourcePath(createLink(v38config), query, 'read')).toBe(
`${link.versionedApiPath}/tracker`
`${apiPath}/tracker/test`
)
})
})
25 changes: 1 addition & 24 deletions engine/src/links/RestAPILink/queryToResourcePath.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { DataEngineConfig } from '../../types/DataEngineConfig'
import type { FetchType } from '../../types/ExecuteOptions'
import type { ResolvedResourceQuery } from '../../types/Query'
import type {
Expand Down Expand Up @@ -71,38 +70,16 @@ const makeActionPath = (resource: string) =>
`${resource.substring(actionPrefix.length)}.action`
)

const skipApiVersion = (
resource: string,
config: DataEngineConfig
): boolean => {
if (resource === 'tracker' || resource.startsWith('tracker/')) {
if (!config.serverVersion?.minor || config.serverVersion?.minor < 38) {
return true
}
}

// The `/api/ping` endpoint is unversioned
if (resource === 'ping') {
return true
}

return false
}

export const queryToResourcePath = (
link: RestAPILink,
query: ResolvedResourceQuery,
type: FetchType
): string => {
const { resource, id, params = {} } = query

const apiBase = skipApiVersion(resource, link.config)
? link.unversionedApiPath
: link.versionedApiPath

const base = isAction(resource)
? makeActionPath(resource)
: joinPath(apiBase, resource, id)
: joinPath(link.apiPath, resource, id)

validateResourceQuery(query, type)

Expand Down
1 change: 0 additions & 1 deletion engine/src/types/DataEngineConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface DataEngineConfig {
baseUrl: string
apiVersion: number
serverVersion?: {
major: number
minor: number
Expand Down
1 change: 0 additions & 1 deletion examples/cra/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { SwitchableProvider } from './components/SwitchableProvider'

const config = {
baseUrl: process.env.REACT_APP_D2_BASE_URL || 'http://localhost:8080',
apiVersion: process.env.REACT_APP_D2_API_VERSION || 33,
}

const providerType = (
Expand Down
2 changes: 0 additions & 2 deletions examples/cra/src/components/ConfigConsumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const ConfigConsumer = () => {
return (
<span>
<strong>Base url:</strong> {config.baseUrl}
<br />
<strong>API version:</strong> {config.apiVersion}
</span>
)
}
9 changes: 1 addition & 8 deletions examples/cra/src/components/SwitchableProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ import React from 'react'

export const SwitchableProvider = ({ type, config, children }) => {
if (type === 'data') {
return (
<DataProvider
baseUrl={config.baseUrl}
apiVersion={config.apiVersion}
>
{children}
</DataProvider>
)
return <DataProvider baseUrl={config.baseUrl}>{children}</DataProvider>
} else if (type === 'runtime') {
return <Provider config={config}>{children}</Provider>
}
Expand Down
8 changes: 2 additions & 6 deletions examples/standalone-engine-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict'

// Simple test script for @dhis2/data-engine
// Usage: node node-engine-test.js --url https://play.dhis2.org/2.37.8 --apiKey <KEY> [--apiVersion 41]
// Usage: node node-engine-test.js --url https://play.dhis2.org/2.37.8 --apiKey <KEY>

const { DataEngine, RestAPILink } = require('@dhis2/data-engine')

Expand All @@ -19,17 +19,14 @@ const parseArg = (name, short) => {

const baseUrl = parseArg('url', 'u') || parseArg('baseUrl', 'b')
const apiKey = parseArg('apiKey', 'k') || parseArg('api-key', 'k')
const apiVersionArg = parseArg('apiVersion', 'v')

if (!baseUrl || !apiKey) {
console.error(
'Usage: node node-engine-test.js --url <baseUrl> --apiKey <key> [--apiVersion <num>]'
'Usage: node node-engine-test.js --url <baseUrl> --apiKey <key>'
)
process.exit(2)
}

const apiVersion = apiVersionArg ? Number(apiVersionArg) : 41

// Ensure global fetch exists (Node 18+). If not present, instruct the user.
if (typeof fetch === 'undefined') {
console.error(
Expand All @@ -42,7 +39,6 @@ async function main() {
try {
const config = {
baseUrl,
apiVersion,
apiToken: apiKey,
}

Expand Down
1 change: 0 additions & 1 deletion services/config/src/ConfigContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ import { Config } from './types'

export const ConfigContext = React.createContext<Config>({
baseUrl: '..',
apiVersion: 32,
})
7 changes: 2 additions & 5 deletions services/config/src/__tests__/integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Config } from '../types'

const mockConfig: Config = {
baseUrl: 'http://test.com',
apiVersion: 42,
serverVersion: {
full: '2.35-SNAPSHOT',
major: 2,
Expand All @@ -23,9 +22,7 @@ const mockConfig: Config = {

describe('Testing custom config provider', () => {
it('Should render without failing', async () => {
const consumerFunction = jest.fn(
(config) => `${config.baseUrl}:${config.apiVersion}`
)
const consumerFunction = jest.fn((config) => `${config.baseUrl}`)
const { getByText } = render(
<ConfigProvider config={mockConfig}>
<ConfigContext.Consumer>
Expand All @@ -34,7 +31,7 @@ describe('Testing custom config provider', () => {
</ConfigProvider>
)

expect(getByText(/http:\/\/test.com:42/i)).not.toBeUndefined()
expect(getByText(/http:\/\/test.com/i)).not.toBeUndefined()
expect(consumerFunction).toHaveBeenCalledTimes(1)
expect(consumerFunction).toHaveBeenLastCalledWith(mockConfig)
})
Expand Down
4 changes: 2 additions & 2 deletions services/config/src/__tests__/useTimeZoneConversion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook } from '@testing-library/react'
import React, { ReactNode } from 'react'
import { ConfigProvider, useTimeZoneConversion } from '../index'

const defaultConfig = { baseUrl: '/', apiVersion: 40 }
const defaultConfig = { baseUrl: '/' }
const defaultSystemInfo = {
version: '40',
contextPath: '',
Expand All @@ -13,7 +13,7 @@ const defaultSystemInfo = {

describe('useTimeZoneConversion', () => {
it('Hook returns a fromClientDate and fromServerDate function', () => {
const config = { baseUrl: '/', apiVersion: 30 }
const config = { baseUrl: '/' }
const wrapper = ({ children }: { children?: ReactNode }) => (
<ConfigProvider config={config}>{children}</ConfigProvider>
)
Expand Down
1 change: 0 additions & 1 deletion services/config/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface SystemInfo {

export interface Config {
baseUrl: string
apiVersion: number
appName?: string
appVersion?: Version
serverVersion?: Version
Expand Down
2 changes: 1 addition & 1 deletion services/data/src/react/components/DataProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('DataProvider', () => {
it('Should pass a new engine and RestAPILink to consumers', () => {
const renderFunction = jest.fn()
render(
<DataProvider baseUrl="test" apiVersion={42}>
<DataProvider baseUrl="test">
<DataContext.Consumer>{renderFunction}</DataContext.Consumer>
</DataProvider>
)
Expand Down
1 change: 0 additions & 1 deletion services/data/src/react/components/DataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { DataContext } from '../context/DataContext'

export interface ProviderInput {
baseUrl?: string
apiVersion?: number
children: React.ReactNode
}

Expand Down
1 change: 0 additions & 1 deletion services/data/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface ContextType {

export interface ContextInput {
baseUrl: string
apiVersion: number
}

export type ExecuteOptions = QueryExecuteOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const wrapper: React.FC<{ children?: React.ReactNode }> = ({ children }) => (
<ConfigProvider
config={{
baseUrl: '..',
apiVersion: 42,
// ensure this is a server version where pings are enabled
serverVersion: { major: 2, minor: 40, patch: 0, full: 'n/a' },
}}
Expand Down Expand Up @@ -779,7 +778,6 @@ describe('lastConnected status', () => {
<ConfigProvider
config={{
baseUrl: '..',
apiVersion: 42,
serverVersion: {
major: 2,
minor: 40,
Expand Down Expand Up @@ -845,7 +843,6 @@ describe('lastConnected status', () => {
<ConfigProvider
config={{
baseUrl: '..',
apiVersion: 42,
appName: testAppName,
serverVersion: {
major: 2,
Expand Down Expand Up @@ -897,7 +894,6 @@ describe("when the /api/ping endpoint isn't supported", () => {
<ConfigProvider
config={{
baseUrl: '..',
apiVersion: 42,
// an unsupported version:
serverVersion: { major: 2, minor: 39, patch: 0, full: 'n/a' },
}}
Expand Down
Loading