44
55import { Translator , checkStatusCode } from './translator' ;
66import {
7+ BaseRequestOptions ,
78 DeepLClientOptions ,
89 WriteResult ,
910 MultilingualGlossaryDictionaryEntries ,
@@ -17,8 +18,8 @@ import {
1718 SpokenTermsList ,
1819 UpdateSpokenTermsOptions ,
1920 StyleProfile ,
20- StyleProfileOptions ,
21- StyleProfileUpdateOptions ,
21+ CreateStyleProfileOptions ,
22+ UpdateStyleProfileOptions ,
2223 StyleRuleInfo ,
2324 StyleId ,
2425 CustomInstruction ,
@@ -70,6 +71,7 @@ import {
7071 extractSpokenTermsId ,
7172 extractStyleProfileId ,
7273 extractTranslationMemoryId ,
74+ isString ,
7375 logInfo ,
7476 resolveExtraBodyParameters ,
7577 setExtraBodyParameters ,
@@ -92,25 +94,31 @@ function spokenTermsEntriesToJson(entries: SpokenTermsEntries): Record<string, u
9294
9395function addExtraBodyParametersToJson (
9496 body : Record < string , unknown > ,
95- options : CreateSpokenTermsOptions | undefined ,
97+ options : BaseRequestOptions | undefined ,
9698) : Record < string , unknown > {
9799 // Added last, so they replace library-set fields with the same key
98100 const extraBodyParameters = resolveExtraBodyParameters ( options ) ;
99101 return extraBodyParameters === undefined ? body : { ...body , ...extraBodyParameters } ;
100102}
101103
102104function styleProfileOptionsToJson (
103- options : StyleProfileOptions | undefined ,
105+ options : CreateStyleProfileOptions | undefined ,
104106) : Record < string , unknown > {
105107 const body : Record < string , unknown > = { } ;
106108 if ( options === undefined ) return body ;
107109 // Unset fields are not sent; '' and [] are sent, as they clear or unlink in an update
108110 if ( options . description !== undefined ) body . description = options . description ;
109111 if ( options . industry !== undefined ) body . industry = options . industry ;
110- if ( options . glossaryIds !== undefined ) body . glossary_ids = options . glossaryIds ;
111- if ( options . styleRuleIds !== undefined ) body . style_rule_ids = options . styleRuleIds ;
112+ if ( options . glossaryIds !== undefined ) {
113+ body . glossary_ids = options . glossaryIds . map ( extractGlossaryId ) ;
114+ }
115+ if ( options . styleRuleIds !== undefined ) {
116+ body . style_rule_ids = options . styleRuleIds . map ( ( styleRule ) =>
117+ isString ( styleRule ) ? styleRule : styleRule . styleId ,
118+ ) ;
119+ }
112120 if ( options . translationMemoryIds !== undefined ) {
113- body . translation_memory_ids = options . translationMemoryIds ;
121+ body . translation_memory_ids = options . translationMemoryIds . map ( extractTranslationMemoryId ) ;
114122 }
115123 if ( options . glossaryPriorityOrderEnabled !== undefined ) {
116124 body . glossary_priority_order_enabled = options . glossaryPriorityOrderEnabled ;
@@ -1825,13 +1833,19 @@ export class DeepLClient extends Translator {
18251833 *
18261834 * @param name: Name of the style profile.
18271835 * @param options: Description, industry and the IDs of the glossaries, style rule lists and
1828- * translation memories to link, see {@link StyleProfileOptions }.
1836+ * translation memories to link, see {@link CreateStyleProfileOptions }.
18291837 * @returns {Promise<StyleProfile> } The created style profile.
18301838 *
18311839 * @throws {DeepLError } If any error occurs while communicating with the DeepL API.
18321840 */
1833- async createStyleProfile ( name : string , options ?: StyleProfileOptions ) : Promise < StyleProfile > {
1834- const body = { name, ...styleProfileOptionsToJson ( options ) } ;
1841+ async createStyleProfile (
1842+ name : string ,
1843+ options ?: CreateStyleProfileOptions ,
1844+ ) : Promise < StyleProfile > {
1845+ const body = addExtraBodyParametersToJson (
1846+ { name, ...styleProfileOptionsToJson ( options ) } ,
1847+ options ,
1848+ ) ;
18351849 const { statusCode, content } = await this . httpClient . sendRequestWithBackoff < string > (
18361850 'POST' ,
18371851 '/v3/style_profiles' ,
@@ -1862,23 +1876,25 @@ export class DeepLClient extends Translator {
18621876 /**
18631877 * Updates a style profile. Options left undefined are not changed. An empty string clears
18641878 * the description or industry; an ID array replaces the linked components of that kind in
1865- * full, and an empty array unlinks all of them. With no options set, nothing changes.
1879+ * full, and an empty array unlinks all of them. With no options set, `{}` is sent and nothing
1880+ * changes.
18661881 *
18671882 * @param styleProfile: ID of the style profile, or a StyleProfile object.
1868- * @param options: The fields to change, see {@link StyleProfileUpdateOptions }.
1883+ * @param options: The fields to change (optional) , see {@link UpdateStyleProfileOptions }.
18691884 * @returns {Promise<StyleProfile> } The updated style profile.
18701885 *
18711886 * @throws {DeepLError } If any error occurs while communicating with the DeepL API.
18721887 */
18731888 async updateStyleProfile (
18741889 styleProfile : string | StyleProfile ,
1875- options : StyleProfileUpdateOptions ,
1890+ options ?: UpdateStyleProfileOptions ,
18761891 ) : Promise < StyleProfile > {
18771892 const styleProfileId = extractStyleProfileId ( styleProfile ) ;
1878- const body = styleProfileOptionsToJson ( options ) ;
1879- if ( options . name !== undefined ) {
1880- body . name = options . name ;
1893+ const fields = styleProfileOptionsToJson ( options ) ;
1894+ if ( options ? .name !== undefined ) {
1895+ fields . name = options . name ;
18811896 }
1897+ const body = addExtraBodyParametersToJson ( fields , options ) ;
18821898 const { statusCode, content } = await this . httpClient . sendRequestWithBackoff < string > (
18831899 'PATCH' ,
18841900 `/v3/style_profiles/${ encodeURIComponent ( styleProfileId ) } ` ,
0 commit comments