Skip to content

Commit 3449791

Browse files
hive-botclaude
andcommitted
fix: send configured rules and custom instructions as given in createStyleRule()
The positional configuredRules and customInstructions of createStyleRule() take the API's shape (configured_rules, custom_instructions with source_language), like updateStyleRuleConfiguredRules(), and are sent unchanged. CustomInstructionInput is removed and CustomInstructionRequestBody is no longer deprecated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8034865 commit 3449791

5 files changed

Lines changed: 41 additions & 84 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6868
- `createStyleRule()` accepts the name, language, configured rules and custom
6969
instructions as separate arguments:
7070
`createStyleRule(name, language, configuredRules?, customInstructions?)`.
71-
`configuredRules` is a `ConfiguredRules` object (camelCase categories such as
72-
`datesAndTimes`) and `customInstructions` is an array of the new
73-
`CustomInstructionInput` type (`label`, `prompt`, `sourceLanguage`). They are
74-
sent with the API's snake_case names.
71+
They use the API's names and are sent as given: `configuredRules` as
72+
`configured_rules` (e.g. `{ dates_and_times: { calendar_era: 'use_bc_and_ad' } }`,
73+
like `updateStyleRuleConfiguredRules()`), and `customInstructions` as
74+
`custom_instructions` (`CustomInstructionRequestBody` objects with `label`,
75+
`prompt` and `source_language`).
7576
- `createStyleRuleCustomInstruction()` and `updateStyleRuleCustomInstruction()`
7677
accept the label, prompt and optional source language as separate arguments:
7778
`createStyleRuleCustomInstruction(styleId, label, prompt, sourceLanguage?)` and
@@ -103,10 +104,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
103104
argument instead. The positional form will be removed in the next major
104105
version.
105106
- Passing a single `CreateStyleRuleRequestBody` object to `createStyleRule()`
106-
and a `CustomInstructionRequestBody` object to
107+
is deprecated, as is the `CreateStyleRuleRequestBody` type; pass separate
108+
arguments instead. Passing a `CustomInstructionRequestBody` object to
107109
`createStyleRuleCustomInstruction()` or `updateStyleRuleCustomInstruction()`
108-
is deprecated, as are both types; pass separate arguments instead. The object
109-
forms and types will be removed in the next major version.
110+
is deprecated; pass the label, prompt and source language as separate
111+
arguments instead. These object forms and `CreateStyleRuleRequestBody` will be
112+
removed in the next major version.
110113

111114
### Fixed
112115
- `downloadTranslationMemoryExport()` no longer hangs on Node 16 and 17 when the output file

‎README.md‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,12 @@ your account, each with a user-specified name and a uniquely-assigned ID.
590590
#### Creating a style rule
591591

592592
Use `createStyleRule()` to create a new style rule with a name and language
593-
code. You can optionally provide `configuredRules` (a `ConfiguredRules` object,
594-
keyed by category such as `datesAndTimes` or `styleAndTone`) and
595-
`customInstructions` (an array of `CustomInstructionInput` objects with `label`,
596-
`prompt` and an optional `sourceLanguage`).
593+
code. You can optionally provide `configuredRules` and `customInstructions`,
594+
using the API's names: `configuredRules` is sent as given as `configured_rules`
595+
(the same shape `updateStyleRuleConfiguredRules()` takes, e.g.
596+
`{ dates_and_times: { calendar_era: 'use_bc_and_ad' } }`), and
597+
`customInstructions` as `custom_instructions`, an array of objects with
598+
`label`, `prompt` and an optional `source_language`.
597599

598600
```javascript
599601
const rule = await deeplClient.createStyleRule('My Style Rule', 'en');
@@ -602,14 +604,14 @@ console.log(`Created: ${rule.name} (${rule.styleId})`);
602604
const configured = await deeplClient.createStyleRule(
603605
'Formal English',
604606
'en',
605-
{ datesAndTimes: { calendar_era: 'use_bc_and_ad' } },
606-
[{ label: 'Formal tone', prompt: 'Always use formal language', sourceLanguage: 'de' }],
607+
{ dates_and_times: { calendar_era: 'use_bc_and_ad' } },
608+
[{ label: 'Formal tone', prompt: 'Always use formal language', source_language: 'de' }],
607609
);
608610
```
609611

610-
Passing a single object with the wire names (`configured_rules`,
611-
`custom_instructions`, `source_language`) is deprecated and will be removed in
612-
the next major version.
612+
Passing a single `CreateStyleRuleRequestBody` object
613+
(`{ name, language, configured_rules, custom_instructions }`) is deprecated and
614+
will be removed in the next major version.
613615

614616
#### Retrieving and listing style rules
615617

‎src/deeplClient.ts‎

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import {
2323
StyleRuleInfoApiResponse,
2424
StyleId,
2525
CustomInstruction,
26-
CustomInstructionInput,
27-
ConfiguredRules,
2826
TranslationMemoryId,
2927
TranslationMemoryInfo,
3028
TranslationMemoryExport,
@@ -107,12 +105,6 @@ function styleProfileOptionsToJson(
107105
return body;
108106
}
109107

110-
/**
111-
* @deprecated Pass the label, prompt and source language as separate arguments to
112-
* `createStyleRuleCustomInstruction()` and `updateStyleRuleCustomInstruction()`, and use
113-
* {@link CustomInstructionInput} with `createStyleRule()`. This type will be removed in the next
114-
* major version.
115-
*/
116108
export type CustomInstructionRequestBody = {
117109
label: string;
118110
prompt: string;
@@ -140,19 +132,6 @@ function customInstructionToWire(
140132
return body;
141133
}
142134

143-
// Converts the camelCase category keys (e.g. datesAndTimes) to the wire's snake_case; the rule
144-
// names and values inside each category are sent unchanged.
145-
function configuredRulesToWire(
146-
configuredRules: ConfiguredRules,
147-
): Record<string, Record<string, string>> {
148-
const body: Record<string, Record<string, string>> = {};
149-
for (const [category, rules] of Object.entries(configuredRules)) {
150-
if (rules === undefined) continue;
151-
body[category.replace(/[A-Z]/g, (c) => `_${c.toLowerCase()}`)] = rules;
152-
}
153-
return body;
154-
}
155-
156135
export enum WritingStyle {
157136
ACADEMIC = 'academic',
158137
BUSINESS = 'business',
@@ -1487,17 +1466,19 @@ export class DeepLClient extends Translator {
14871466
*
14881467
* @param name: The name of the style rule.
14891468
* @param language: The language code of the style rule.
1490-
* @param configuredRules: Optional predefined rules to enable, see {@link ConfiguredRules}.
1491-
* @param customInstructions: Optional custom instructions, see {@link CustomInstructionInput}.
1469+
* @param configuredRules: Optional configured rules, sent as given as `configured_rules`,
1470+
* e.g. `{ dates_and_times: { calendar_era: 'use_bc_and_ad' } }`.
1471+
* @param customInstructions: Optional custom instructions, sent as given as
1472+
* `custom_instructions`, e.g. `[{ label, prompt, source_language: 'de' }]`.
14921473
* @returns {Promise<StyleRuleInfo>} The created style rule info.
14931474
*
14941475
* @throws {DeepLError} If any error occurs while communicating with the DeepL API.
14951476
*/
14961477
async createStyleRule(
14971478
name: string,
14981479
language: string,
1499-
configuredRules?: ConfiguredRules,
1500-
customInstructions?: CustomInstructionInput[],
1480+
configuredRules?: Record<string, Record<string, string>>,
1481+
customInstructions?: CustomInstructionRequestBody[],
15011482
): Promise<StyleRuleInfo>;
15021483
/**
15031484
* @deprecated Pass the name, language, configured rules and custom instructions as separate
@@ -1507,24 +1488,14 @@ export class DeepLClient extends Translator {
15071488
async createStyleRule(
15081489
nameOrStyleRule: string | CreateStyleRuleRequestBody,
15091490
language?: string,
1510-
configuredRules?: ConfiguredRules,
1511-
customInstructions?: CustomInstructionInput[],
1491+
configuredRules?: Record<string, Record<string, string>>,
1492+
customInstructions?: CustomInstructionRequestBody[],
15121493
): Promise<StyleRuleInfo> {
15131494
let body: CreateStyleRuleRequestBody;
15141495
if (typeof nameOrStyleRule === 'string') {
15151496
body = { name: nameOrStyleRule, language: language as string };
1516-
if (configuredRules !== undefined) {
1517-
body.configured_rules = configuredRulesToWire(configuredRules);
1518-
}
1519-
if (customInstructions !== undefined) {
1520-
body.custom_instructions = customInstructions.map((instruction) =>
1521-
customInstructionToWire(
1522-
instruction.label,
1523-
instruction.prompt,
1524-
instruction.sourceLanguage,
1525-
),
1526-
);
1527-
}
1497+
if (configuredRules !== undefined) body.configured_rules = configuredRules;
1498+
if (customInstructions !== undefined) body.custom_instructions = customInstructions;
15281499
} else {
15291500
body = nameOrStyleRule;
15301501
}

‎src/types.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -870,18 +870,6 @@ export interface CustomInstruction {
870870
readonly sourceLanguage?: string;
871871
}
872872

873-
/**
874-
* Custom instruction to create or update on a style rule.
875-
*/
876-
export interface CustomInstructionInput {
877-
/** Label for the custom instruction. */
878-
readonly label: string;
879-
/** Prompt text for the custom instruction. */
880-
readonly prompt: string;
881-
/** Optional source language code for the custom instruction. */
882-
readonly sourceLanguage?: string;
883-
}
884-
885873
/**
886874
* Configuration rules for a style rule list.
887875
*/

‎tests/offline/styleRules.test.ts‎

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,33 +80,26 @@ describe('DeepLClient.createStyleRule', () => {
8080
await makeDeeplClient().createStyleRule(body);
8181
});
8282

83-
it('sends the same JSON for the positional arguments, converting them to the wire names', async () => {
83+
it('sends the positional configured rules and custom instructions unchanged', async () => {
84+
const configuredRules = { dates_and_times: { calendar_era: 'use_bc_and_ad' } };
85+
const customInstructions = [
86+
{ label: 'Label', prompt: 'Prompt', source_language: 'de' },
87+
{ label: 'L2', prompt: 'P2' },
88+
];
8489
nock(serverUrl)
8590
.post('/v3/style_rules', {
8691
name: 'my rule',
8792
language: 'en',
88-
configured_rules: {
89-
dates_and_times: { calendar_era: 'use_bc_and_ad' },
90-
spelling_and_grammar: { color: 'british' },
91-
},
92-
custom_instructions: [
93-
{ label: 'Label', prompt: 'Prompt', source_language: 'de' },
94-
{ label: 'L2', prompt: 'P2' },
95-
],
93+
configured_rules: configuredRules,
94+
custom_instructions: customInstructions,
9695
})
9796
.reply(201, styleRuleReply);
9897

9998
const styleRule = await makeDeeplClient().createStyleRule(
10099
'my rule',
101100
'en',
102-
{
103-
datesAndTimes: { calendar_era: 'use_bc_and_ad' },
104-
spellingAndGrammar: { color: 'british' },
105-
},
106-
[
107-
{ label: 'Label', prompt: 'Prompt', sourceLanguage: 'de' },
108-
{ label: 'L2', prompt: 'P2' },
109-
],
101+
configuredRules,
102+
customInstructions,
110103
);
111104

112105
expect(styleRule.styleId).toBe('s-1');

0 commit comments

Comments
 (0)