Skip to content

Commit

Permalink
merge: ts-api into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Dec 13, 2024
2 parents 0f4a6f3 + ce741d8 commit a9d9758
Show file tree
Hide file tree
Showing 41 changed files with 446 additions and 426 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

- Sidebar: Collapse and expand attribute sections [#199](https://github.com/spraakbanken/korp-frontend/issues/199)

### Changed

- The `corpus_config_url` setting is replaced by `get_corpus_ids`, see [doc/frontend_devel.md](./doc/frontend_devel.md)
- The `httpConfAddMethod*` util functions were refactored:
- The `$.ajax` case of `httpConfAddMethod` was extracted into `ajaxConfAddMethod`
- `httpConfAddMethodFetch` was renamed to `fetchConfAddMethod`
- `httpConfAddMethodAngular` was removed

## [9.7.2] - 2024-12-09

### Added
Expand Down
46 changes: 11 additions & 35 deletions app/scripts/backend/backend.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
/** @format */
import _ from "lodash"
import { getAuthorizationHeader } from "@/components/auth/auth"
import { KorpResponse, WithinParameters } from "@/backend/types"
import { SavedSearch } from "@/local-storage"
import settings from "@/settings"
import { httpConfAddMethodFetch } from "@/util"
import { KorpStatsParams, KorpStatsResponse, normalizeStatsData } from "@/backend/stats-proxy"
import { normalizeStatsData } from "@/backend/stats-proxy"
import { MapResult, parseMapData } from "@/map_services"
import { KorpQueryResponse } from "@/backend/kwic-proxy"

type KorpLoglikeResponse = {
/** Log-likelihood average. */
average: number
/** Log-likelihood values. */
loglike: Record<string, number>
/** Absolute frequency for the values in set 1. */
set1: Record<string, number>
/** Absolute frequency for the values in set 2. */
set2: Record<string, number>
}
import { korpRequest } from "./common"
import { Response, WithinParameters } from "./types"
import { QueryResponse } from "./types/query"
import { CountParams } from "./types/count"

export type CompareResult = [CompareTables, number, SavedSearch, SavedSearch, string[]]

Expand Down Expand Up @@ -52,16 +41,6 @@ export type MapRequestResult = {

type MapAttribute = { label: string; corpora: string[] }

async function korpRequest<T extends Record<string, any> = {}, P extends Record<string, any> = {}>(
endpoint: string,
params: P
): Promise<KorpResponse<T>> {
const { url, request } = httpConfAddMethodFetch(settings.korp_backend_url + "/" + endpoint, params)
request.headers = { ...request.headers, ...getAuthorizationHeader() }
const response = await fetch(url, request)
return (await response.json()) as KorpResponse<T>
}

/** Note: since this is using native Promise, we must use it with something like $q or $scope.$apply for AngularJS to react when they resolve. */
export async function requestCompare(
cmpObj1: SavedSearch,
Expand All @@ -86,12 +65,12 @@ export async function requestCompare(
set1_cqp: cmpObj1.cqp,
set2_corpus: corpora2.join(",").toUpperCase(),
set2_cqp: cmpObj2.cqp,
max: "50",
max: 50,
split,
top,
}

const data = await korpRequest<KorpLoglikeResponse>("loglike", params)
const data = await korpRequest("loglike", params)

if ("ERROR" in data) {
// TODO Create a KorpBackendError which could be displayed nicely
Expand Down Expand Up @@ -134,7 +113,7 @@ export async function requestMapData(
attribute: MapAttribute,
relative?: boolean
): Promise<MapRequestResult> {
const params: KorpStatsParams = {
const params: CountParams = {
group_by_struct: attribute.label,
cqp,
corpus: attribute.corpora.join(","),
Expand All @@ -146,7 +125,7 @@ export async function requestMapData(

Object.keys(cqpExprs).map((cqp, i) => (params[`subcqp${i}`] = cqp))

const data = await korpRequest<KorpStatsResponse>("count", params)
const data = await korpRequest("count", params)

if ("ERROR" in data) {
// TODO Create a KorpBackendError which could be displayed nicely
Expand All @@ -158,10 +137,7 @@ export async function requestMapData(
return { corpora: attribute.corpora, cqp, within, data: result, attribute }
}

export async function getDataForReadingMode(
inputCorpus: string,
textId: string
): Promise<KorpResponse<KorpQueryResponse>> {
export async function getDataForReadingMode(inputCorpus: string, textId: string): Promise<Response<QueryResponse>> {
const corpus = inputCorpus.toUpperCase()
const corpusSettings = settings.corpusListing.get(inputCorpus)

Expand All @@ -184,5 +160,5 @@ export async function getDataForReadingMode(
end: 0,
}

return korpRequest<KorpQueryResponse>("query", params)
return korpRequest("query", params)
}
27 changes: 0 additions & 27 deletions app/scripts/backend/base-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,6 @@ export default abstract class BaseProxy<R extends {} = {}> {
this.total = null
}

/**
* Return a URL with baseUrl base and data encoded as URL parameters.
* If baseUrl already contains URL parameters, return it as is.
*
* Note that this function is now largely redundant: when called
* for GET URLs already containing URL parameters, it does
* nothing, whereas the GET URL returned by it for a POST URL
* typically results in an "URI too long" error, if
* settings.backendURLMaxLength is configured appropriately for
* the Web server on which the backend runs.
*/
makeUrlWithParams(baseUrl: string, data: Record<string, any>): string {
if (baseUrl.indexOf("?") != -1) {
return baseUrl
}
return (
baseUrl +
"?" +
_.toPairs(data)
.map(function ([key, val]) {
val = encodeURIComponent(val)
return key + "=" + val
})
.join("&")
)
}

abort(): void {
this.pendingRequests.forEach((req) => req.abort())
this.cleanup()
Expand Down
17 changes: 17 additions & 0 deletions app/scripts/backend/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @format */
import { fetchConfAddMethod } from "@/util"
import { getAuthorizationHeader } from "@/components/auth/auth"
import settings from "@/settings"
import { API, Response } from "./types"
import { omitBy } from "lodash"

export async function korpRequest<K extends keyof API>(
endpoint: K,
params: API[K]["params"]
): Promise<Response<API[K]["response"]>> {
params = omitBy(params, (value) => value == null) as API[K]["params"]
const { url, request } = fetchConfAddMethod(settings.korp_backend_url + "/" + endpoint, params)
request.headers = { ...request.headers, ...getAuthorizationHeader() }
const response = await fetch(url, request)
return (await response.json()) as Response<API[K]["response"]>
}
20 changes: 9 additions & 11 deletions app/scripts/backend/graph-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import _ from "lodash"
import settings from "@/settings"
import BaseProxy from "@/backend/base-proxy"
import { AjaxSettings, Granularity, Histogram, KorpResponse, NumericString } from "@/backend/types"
import { AbsRelTuple } from "@/statistics.types"
import { Factory, httpConfAddMethod } from "@/util"
import { AbsRelTuple, Granularity, Histogram, Response, NumericString } from "@/backend/types"
import { ajaxConfAddMethod, Factory } from "@/util"
import { AjaxSettings } from "@/jquery.types"

export class GraphProxy extends BaseProxy<KorpCountTimeResponse> {
granularity: Granularity
prevParams: KorpCountTimeParams | null
prevRequest: AjaxSettings

constructor() {
super()
Expand All @@ -33,7 +32,7 @@ export class GraphProxy extends BaseProxy<KorpCountTimeResponse> {
corpora: string,
from: NumericString,
to: NumericString
): JQuery.Promise<KorpResponse<KorpCountTimeResponse>> {
): JQuery.Promise<Response<KorpCountTimeResponse>> {
this.resetRequest()
const self = this
const params: KorpCountTimeParams = {
Expand All @@ -56,13 +55,12 @@ export class GraphProxy extends BaseProxy<KorpCountTimeResponse> {
this.prevParams = params
const def = $.Deferred()

const ajaxSettings: AjaxSettings = {
const ajaxSettings = {
url: settings.korp_backend_url + "/count_time",
dataType: "json",
data: params,

beforeSend: (req, settings) => {
this.prevRequest = settings
beforeSend: (req) => {
this.addAuthorizationHeader(req)
},

Expand All @@ -77,13 +75,13 @@ export class GraphProxy extends BaseProxy<KorpCountTimeResponse> {
error(jqXHR, textStatus, errorThrown) {
def.reject(textStatus)
},
success(data: KorpResponse<KorpCountTimeResponse>) {
success(data: Response<KorpCountTimeResponse>) {
def.resolve(data)
self.cleanup()
},
}
} satisfies AjaxSettings

$.ajax(httpConfAddMethod(ajaxSettings))
$.ajax(ajaxConfAddMethod(ajaxSettings))

return def.promise()
}
Expand Down
Loading

0 comments on commit a9d9758

Please sign in to comment.