From cd15a0e966f5e90f01f9dfd926e2f7709babe943 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 21 Oct 2022 19:51:38 +0200 Subject: [PATCH] jsonrpc: typescript client: export constants under `C` enum, similar to how its exported from `deltachat-node` (#3681) * jsonrpc: typescript client: export constants under `C` enum, similar to how its exported from `deltachat-node` * add pr number to changelog * fix tests * fix changelog entry position --- CHANGELOG.md | 1 + .../typescript/generated/constants.ts | 201 ++++++++++++++++++ deltachat-jsonrpc/typescript/package.json | 3 +- .../typescript/scripts/generate-constants.js | 53 +++++ deltachat-jsonrpc/typescript/src/lib.ts | 1 + 5 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 deltachat-jsonrpc/typescript/generated/constants.ts create mode 100755 deltachat-jsonrpc/typescript/scripts/generate-constants.js diff --git a/CHANGELOG.md b/CHANGELOG.md index e7677e674..b7365a560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased ### API-Changes +- jsonrpc: typescript client: export constants under `C` enum, similar to how its exported from `deltachat-node` #3681 ### Changes - simplify `UPSERT` queries #3676 diff --git a/deltachat-jsonrpc/typescript/generated/constants.ts b/deltachat-jsonrpc/typescript/generated/constants.ts new file mode 100644 index 000000000..3f50b6653 --- /dev/null +++ b/deltachat-jsonrpc/typescript/generated/constants.ts @@ -0,0 +1,201 @@ +// Generated! + +export enum C { + DC_CERTCK_ACCEPT_INVALID_CERTIFICATES = 3, + DC_CERTCK_AUTO = 0, + DC_CERTCK_STRICT = 1, + DC_CHAT_ID_ALLDONE_HINT = 7, + DC_CHAT_ID_ARCHIVED_LINK = 6, + DC_CHAT_ID_LAST_SPECIAL = 9, + DC_CHAT_ID_TRASH = 3, + DC_CHAT_TYPE_BROADCAST = 160, + DC_CHAT_TYPE_GROUP = 120, + DC_CHAT_TYPE_MAILINGLIST = 140, + DC_CHAT_TYPE_SINGLE = 100, + DC_CHAT_TYPE_UNDEFINED = 0, + DC_CONNECTIVITY_CONNECTED = 4000, + DC_CONNECTIVITY_CONNECTING = 2000, + DC_CONNECTIVITY_NOT_CONNECTED = 1000, + DC_CONNECTIVITY_WORKING = 3000, + DC_CONTACT_ID_DEVICE = 5, + DC_CONTACT_ID_INFO = 2, + DC_CONTACT_ID_LAST_SPECIAL = 9, + DC_CONTACT_ID_SELF = 1, + DC_GCL_ADD_ALLDONE_HINT = 4, + DC_GCL_ADD_SELF = 2, + DC_GCL_ARCHIVED_ONLY = 1, + DC_GCL_FOR_FORWARDING = 8, + DC_GCL_NO_SPECIALS = 2, + DC_GCL_VERIFIED_ONLY = 1, + DC_GCM_ADDDAYMARKER = 1, + DC_GCM_INFO_ONLY = 2, + DC_INFO_PROTECTION_DISABLED = 12, + DC_INFO_PROTECTION_ENABLED = 11, + DC_KEY_GEN_DEFAULT = 0, + DC_KEY_GEN_ED25519 = 2, + DC_KEY_GEN_RSA2048 = 1, + DC_LP_AUTH_NORMAL = 4, + DC_LP_AUTH_OAUTH2 = 2, + DC_MEDIA_QUALITY_BALANCED = 0, + DC_MEDIA_QUALITY_WORSE = 1, + DC_MSG_ID_DAYMARKER = 9, + DC_MSG_ID_LAST_SPECIAL = 9, + DC_MSG_ID_MARKER1 = 1, + DC_PROVIDER_STATUS_BROKEN = 3, + DC_PROVIDER_STATUS_OK = 1, + DC_PROVIDER_STATUS_PREPARATION = 2, + DC_SHOW_EMAILS_ACCEPTED_CONTACTS = 1, + DC_SHOW_EMAILS_ALL = 2, + DC_SHOW_EMAILS_OFF = 0, + DC_SOCKET_AUTO = 0, + DC_SOCKET_PLAIN = 3, + DC_SOCKET_SSL = 1, + DC_SOCKET_STARTTLS = 2, + DC_STATE_IN_FRESH = 10, + DC_STATE_IN_NOTICED = 13, + DC_STATE_IN_SEEN = 16, + DC_STATE_OUT_DELIVERED = 26, + DC_STATE_OUT_DRAFT = 19, + DC_STATE_OUT_FAILED = 24, + DC_STATE_OUT_MDN_RCVD = 28, + DC_STATE_OUT_PENDING = 20, + DC_STATE_OUT_PREPARING = 18, + DC_STATE_UNDEFINED = 0, + DC_STR_AC_SETUP_MSG_BODY = 43, + DC_STR_AC_SETUP_MSG_SUBJECT = 42, + DC_STR_ADD_MEMBER_BY_OTHER = 129, + DC_STR_ADD_MEMBER_BY_YOU = 128, + DC_STR_AEAP_ADDR_CHANGED = 122, + DC_STR_AEAP_EXPLANATION_AND_LINK = 123, + DC_STR_ARCHIVEDCHATS = 40, + DC_STR_AUDIO = 11, + DC_STR_BAD_TIME_MSG_BODY = 85, + DC_STR_BROADCAST_LIST = 115, + DC_STR_CANNOT_LOGIN = 60, + DC_STR_CANTDECRYPT_MSG_BODY = 29, + DC_STR_CONFIGURATION_FAILED = 84, + DC_STR_CONNECTED = 107, + DC_STR_CONNTECTING = 108, + DC_STR_CONTACT_NOT_VERIFIED = 36, + DC_STR_CONTACT_SETUP_CHANGED = 37, + DC_STR_CONTACT_VERIFIED = 35, + DC_STR_DEVICE_MESSAGES = 68, + DC_STR_DEVICE_MESSAGES_HINT = 70, + DC_STR_DOWNLOAD_AVAILABILITY = 100, + DC_STR_DRAFT = 3, + DC_STR_E2E_AVAILABLE = 25, + DC_STR_E2E_PREFERRED = 34, + DC_STR_ENCRYPTEDMSG = 24, + DC_STR_ENCR_NONE = 28, + DC_STR_ENCR_TRANSP = 27, + DC_STR_EPHEMERAL_DAY = 79, + DC_STR_EPHEMERAL_DAYS = 95, + DC_STR_EPHEMERAL_DISABLED = 75, + DC_STR_EPHEMERAL_FOUR_WEEKS = 81, + DC_STR_EPHEMERAL_HOUR = 78, + DC_STR_EPHEMERAL_HOURS = 94, + DC_STR_EPHEMERAL_MINUTE = 77, + DC_STR_EPHEMERAL_MINUTES = 93, + DC_STR_EPHEMERAL_SECONDS = 76, + DC_STR_EPHEMERAL_TIMER_1_DAY_BY_OTHER = 147, + DC_STR_EPHEMERAL_TIMER_1_DAY_BY_YOU = 146, + DC_STR_EPHEMERAL_TIMER_1_HOUR_BY_OTHER = 145, + DC_STR_EPHEMERAL_TIMER_1_HOUR_BY_YOU = 144, + DC_STR_EPHEMERAL_TIMER_1_MINUTE_BY_OTHER = 143, + DC_STR_EPHEMERAL_TIMER_1_MINUTE_BY_YOU = 142, + DC_STR_EPHEMERAL_TIMER_1_WEEK_BY_OTHER = 149, + DC_STR_EPHEMERAL_TIMER_1_WEEK_BY_YOU = 148, + DC_STR_EPHEMERAL_TIMER_DAYS_BY_OTHER = 155, + DC_STR_EPHEMERAL_TIMER_DAYS_BY_YOU = 154, + DC_STR_EPHEMERAL_TIMER_DISABLED_BY_OTHER = 139, + DC_STR_EPHEMERAL_TIMER_DISABLED_BY_YOU = 138, + DC_STR_EPHEMERAL_TIMER_HOURS_BY_OTHER = 153, + DC_STR_EPHEMERAL_TIMER_HOURS_BY_YOU = 152, + DC_STR_EPHEMERAL_TIMER_MINUTES_BY_OTHER = 151, + DC_STR_EPHEMERAL_TIMER_MINUTES_BY_YOU = 150, + DC_STR_EPHEMERAL_TIMER_SECONDS_BY_OTHER = 141, + DC_STR_EPHEMERAL_TIMER_SECONDS_BY_YOU = 140, + DC_STR_EPHEMERAL_TIMER_WEEKS_BY_OTHER = 157, + DC_STR_EPHEMERAL_TIMER_WEEKS_BY_YOU = 156, + DC_STR_EPHEMERAL_WEEK = 80, + DC_STR_EPHEMERAL_WEEKS = 96, + DC_STR_ERROR = 112, + DC_STR_ERROR_NO_NETWORK = 87, + DC_STR_FAILED_SENDING_TO = 74, + DC_STR_FILE = 12, + DC_STR_FINGERPRINTS = 30, + DC_STR_FORWARDED = 97, + DC_STR_GIF = 23, + DC_STR_GROUP_IMAGE_CHANGED_BY_OTHER = 127, + DC_STR_GROUP_IMAGE_CHANGED_BY_YOU = 126, + DC_STR_GROUP_IMAGE_DELETED_BY_OTHER = 135, + DC_STR_GROUP_IMAGE_DELETED_BY_YOU = 134, + DC_STR_GROUP_LEFT_BY_OTHER = 133, + DC_STR_GROUP_LEFT_BY_YOU = 132, + DC_STR_GROUP_NAME_CHANGED_BY_OTHER = 125, + DC_STR_GROUP_NAME_CHANGED_BY_YOU = 124, + DC_STR_IMAGE = 9, + DC_STR_INCOMING_MESSAGES = 103, + DC_STR_LAST_MSG_SENT_SUCCESSFULLY = 111, + DC_STR_LOCATION = 66, + DC_STR_LOCATION_ENABLED_BY_OTHER = 137, + DC_STR_LOCATION_ENABLED_BY_YOU = 136, + DC_STR_MESSAGES = 114, + DC_STR_MSGACTIONBYME = 63, + DC_STR_MSGACTIONBYUSER = 62, + DC_STR_MSGADDMEMBER = 17, + DC_STR_MSGDELMEMBER = 18, + DC_STR_MSGGROUPLEFT = 19, + DC_STR_MSGGRPIMGCHANGED = 16, + DC_STR_MSGGRPIMGDELETED = 33, + DC_STR_MSGGRPNAME = 15, + DC_STR_MSGLOCATIONDISABLED = 65, + DC_STR_MSGLOCATIONENABLED = 64, + DC_STR_NOMESSAGES = 1, + DC_STR_NOT_CONNECTED = 121, + DC_STR_NOT_SUPPORTED_BY_PROVIDER = 113, + DC_STR_ONE_MOMENT = 106, + DC_STR_OUTGOING_MESSAGES = 104, + DC_STR_PARTIAL_DOWNLOAD_MSG_BODY = 99, + DC_STR_PART_OF_TOTAL_USED = 116, + DC_STR_PROTECTION_DISABLED = 89, + DC_STR_PROTECTION_DISABLED_BY_OTHER = 161, + DC_STR_PROTECTION_DISABLED_BY_YOU = 160, + DC_STR_PROTECTION_ENABLED = 88, + DC_STR_PROTECTION_ENABLED_BY_OTHER = 159, + DC_STR_PROTECTION_ENABLED_BY_YOU = 158, + DC_STR_QUOTA_EXCEEDING_MSG_BODY = 98, + DC_STR_READRCPT = 31, + DC_STR_READRCPT_MAILBODY = 32, + DC_STR_REMOVE_MEMBER_BY_OTHER = 131, + DC_STR_REMOVE_MEMBER_BY_YOU = 130, + DC_STR_REPLY_NOUN = 90, + DC_STR_SAVED_MESSAGES = 69, + DC_STR_SECURE_JOIN_GROUP_QR_DESC = 120, + DC_STR_SECURE_JOIN_REPLIES = 118, + DC_STR_SECURE_JOIN_STARTED = 117, + DC_STR_SELF = 2, + DC_STR_SELF_DELETED_MSG_BODY = 91, + DC_STR_SENDING = 110, + DC_STR_SERVER_TURNED_OFF = 92, + DC_STR_SETUP_CONTACT_QR_DESC = 119, + DC_STR_STICKER = 67, + DC_STR_STORAGE_ON_DOMAIN = 105, + DC_STR_SUBJECT_FOR_NEW_CONTACT = 73, + DC_STR_SYNC_MSG_BODY = 102, + DC_STR_SYNC_MSG_SUBJECT = 101, + DC_STR_UNKNOWN_SENDER_FOR_CHAT = 72, + DC_STR_UPDATE_REMINDER_MSG_BODY = 86, + DC_STR_UPDATING = 109, + DC_STR_VIDEO = 10, + DC_STR_VIDEOCHAT_INVITATION = 82, + DC_STR_VIDEOCHAT_INVITE_MSG_BODY = 83, + DC_STR_VOICEMESSAGE = 7, + DC_STR_WELCOME_MESSAGE = 71, + DC_TEXT1_DRAFT = 1, + DC_TEXT1_SELF = 3, + DC_TEXT1_USERNAME = 2, + DC_VIDEOCHATTYPE_BASICWEBRTC = 1, + DC_VIDEOCHATTYPE_JITSI = 2, + DC_VIDEOCHATTYPE_UNKNOWN = 0, +} diff --git a/deltachat-jsonrpc/typescript/package.json b/deltachat-jsonrpc/typescript/package.json index dab8cace9..8aa538f09 100644 --- a/deltachat-jsonrpc/typescript/package.json +++ b/deltachat-jsonrpc/typescript/package.json @@ -28,7 +28,7 @@ "main": "dist/deltachat.js", "name": "@deltachat/jsonrpc-client", "scripts": { - "build": "run-s generate-bindings build:tsc build:bundle", + "build": "run-s generate-bindings extract-constants build:tsc build:bundle", "build:bundle": "esbuild --format=esm --bundle dist/deltachat.js --outfile=dist/deltachat.bundle.js", "build:tsc": "tsc", "docs": "typedoc --out docs deltachat.ts", @@ -37,6 +37,7 @@ "example:dev": "esbuild example/example.ts --bundle --outfile=dist/example.bundle.js --servedir=.", "example:start": "http-server .", "generate-bindings": "cargo test", + "extract-constants": "node ./scripts/generate-constants.js", "prettier:check": "prettier --check **.ts", "prettier:fix": "prettier --write **.ts", "test": "run-s test:prepare test:run-coverage test:report-coverage", diff --git a/deltachat-jsonrpc/typescript/scripts/generate-constants.js b/deltachat-jsonrpc/typescript/scripts/generate-constants.js new file mode 100755 index 000000000..525a9d42b --- /dev/null +++ b/deltachat-jsonrpc/typescript/scripts/generate-constants.js @@ -0,0 +1,53 @@ +#!/usr/bin/env node +import { readFileSync, writeFileSync } from "fs"; +import { resolve } from "path"; +import { fileURLToPath } from "url"; +import { dirname } from "path"; +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +const data = []; +const header = resolve(__dirname, "../../../deltachat-ffi/deltachat.h"); + +console.log("Generating constants..."); + +const header_data = readFileSync(header, "UTF-8"); +const regex = /^#define\s+(\w+)\s+(\w+)/gm; +let match; +while (null != (match = regex.exec(header_data))) { + const key = match[1]; + const value = parseInt(match[2]); + if (!isNaN(value)) { + data.push({ key, value }); + } +} + +const constants = data + .filter( + ({ key }) => key.toUpperCase()[0] === key[0] // check if define name is uppercase + ) + .sort((lhs, rhs) => { + if (lhs.key < rhs.key) return -1; + else if (lhs.key > rhs.key) return 1; + return 0; + }) + .filter(({ key }) => { + // filter out what we don't need it + return !( + key.startsWith("DC_EVENT_") || + key.startsWith("DC_IMEX_") || + key.startsWith("DC_CHAT_VISIBILITY") || + key.startsWith("DC_DOWNLOAD") || + (key.startsWith("DC_MSG") && !key.startsWith("DC_MSG_ID")) || + key.startsWith("DC_QR_") + ); + }) + .map((row) => { + return ` ${row.key}: ${row.value}`; + }) + .join(",\n"); + +writeFileSync( + resolve(__dirname, "../generated/constants.ts"), + `// Generated!\n\nexport enum C {\n${constants.replace(/:/g, " =")},\n}\n` +); diff --git a/deltachat-jsonrpc/typescript/src/lib.ts b/deltachat-jsonrpc/typescript/src/lib.ts index 153c1d6a2..473d2bd33 100644 --- a/deltachat-jsonrpc/typescript/src/lib.ts +++ b/deltachat-jsonrpc/typescript/src/lib.ts @@ -4,3 +4,4 @@ export * from "../generated/events.js"; export { RawClient } from "../generated/client.js"; export * from "./client.js"; export * as yerpc from "yerpc"; +export { C } from "../generated/constants.js";