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

feat: allow graphql for crowdin.com, add croql to listTmSegments #361

Merged
merged 3 commits into from
Jan 9, 2024
Merged
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
14 changes: 7 additions & 7 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
*/
export interface PatchRequest {
/** Patch value */
value?: any;

Check warning on line 80 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

/** Patch operation to perform */
op: PatchOperation;
Expand Down Expand Up @@ -139,7 +139,7 @@
}
}

function isAxiosError(error: any): error is AxiosError {

Check warning on line 142 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
return error instanceof AxiosError || !!error.response?.data;
}

Expand All @@ -147,10 +147,10 @@
* @internal
*/
export function handleHttpClientError(error: HttpClientError): never {
let crowdinResponseErrors: any = null;

Check warning on line 150 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

if (isAxiosError(error)) {
crowdinResponseErrors = (error.response?.data as any)?.errors || (error.response?.data as any)?.error;

Check warning on line 153 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type

Check warning on line 153 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
} else if (error instanceof FetchClientJsonPayloadError) {
crowdinResponseErrors =
error.jsonPayload &&
Expand All @@ -163,13 +163,13 @@
if (Array.isArray(crowdinResponseErrors)) {
const validationCodes: { key: string; codes: string[] }[] = [];
const validationMessages: string[] = [];
crowdinResponseErrors.forEach((e: any) => {

Check warning on line 166 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
if (typeof e.index === 'number' || typeof e.error?.key === 'number') {
throw new CrowdinValidationError(JSON.stringify(crowdinResponseErrors, null, 2), []);
}
if (e.error?.key && Array.isArray(e.error?.errors)) {
const codes: string[] = [];
e.error.errors.forEach((er: any) => {

Check warning on line 172 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
if (er.message && er.code) {
codes.push(er.code);
validationMessages.push(er.message);
Expand Down Expand Up @@ -253,16 +253,16 @@
this.config = config;
}

graphql<T>(req: { query: string; operationName?: string; variables?: any }): Promise<ResponseObject<T>> {

Check warning on line 256 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
if (!this.organization) {
throw new Error('GraphQL API could be used only with Crowdin Enterprise.');
let url;

if (this.organization) {
url = `https://${this.organization}.api.crowdin.com/api/graphql`;

Check warning on line 260 in src/core/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/index.ts#L260

Added line #L260 was not covered by tests
} else {
url = 'https://api.crowdin.com/api/graphql';

Check warning on line 262 in src/core/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/index.ts#L262

Added line #L262 was not covered by tests
}
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved

return this.post<ResponseObject<T>>(
`https://${this.organization}.api.crowdin.com/api/graphql`,
req,
this.defaultConfig(),
);
return this.post<ResponseObject<T>>(url, req, this.defaultConfig());

Check warning on line 265 in src/core/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/index.ts#L265

Added line #L265 was not covered by tests
}

protected addQueryParam(url: string, name: string, value?: string | number): string {
Expand Down Expand Up @@ -314,7 +314,7 @@
return this;
}

protected async getList<T = any>(

Check warning on line 317 in src/core/index.ts

View workflow job for this annotation

GitHub Actions / code-coverage

Unexpected any. Specify a different type
url: string,
limit?: number,
offset?: number,
Expand Down
12 changes: 10 additions & 2 deletions src/translationMemory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ export class TranslationMemory extends CrowdinApi {
* @param options optional paramerers for the request
* @see https://developer.crowdin.com/api/v2/#operation/api.tms.segments.getMany
*/
listTmSegments(tmId: number, options?: PaginationOptions): Promise<ResponseList<TranslationMemoryModel.TMSegment>> {
const url = `${this.url}/tms/${tmId}/segments`;
listTmSegments(
tmId: number,
options?: TranslationMemoryModel.ListSegmentsOptions,
): Promise<ResponseList<TranslationMemoryModel.TMSegment>> {
let url = `${this.url}/tms/${tmId}/segments`;
url = this.addQueryParam(url, 'croql', options?.croql);
return this.getList(url, options?.limit, options?.offset);
}

Expand Down Expand Up @@ -358,6 +362,10 @@ export namespace TranslationMemoryModel {
userId?: number;
}

export interface ListSegmentsOptions extends PaginationOptions {
croql?: string;
}

export interface TMSegment {
id: number;
records: TMSegmentRecord[];
Expand Down
Loading