Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/web-api-placeholder-enums.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slack/web-api": patch
---

fix(web-api): accept string values for ID fields in response types
16 changes: 3 additions & 13 deletions packages/web-api/src/types/response/RtmStartResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,22 @@ export type RtmStartResponse = WebAPICallResult & {
};

export interface Bot {
app_id?: Appid;
app_id?: string;
deleted?: boolean;
icons?: BotIcons;
id?: Id;
id?: string;
is_workflow_bot?: boolean;
name?: string;
team_id?: string;
updated?: number;
}

export enum Appid {
A00000000 = 'A00000000',
Empty = '',
}

export interface BotIcons {
image_36?: string;
image_48?: string;
image_72?: string;
}

export enum Id {
B00000000 = 'B00000000',
Empty = '',
}

export interface Channel {
connected_limited_team_ids?: string[];
connected_team_ids?: string[];
Expand Down Expand Up @@ -2315,7 +2305,7 @@ export interface Profile {
always_active?: boolean;
api_app_id?: string;
avatar_hash?: string;
bot_id?: Id;
bot_id?: string;
display_name?: string;
display_name_normalized?: string;
email?: string;
Expand Down
19 changes: 7 additions & 12 deletions packages/web-api/src/types/response/SearchAllResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface FilesMatch {
is_public?: boolean;
is_restricted_sharing_enabled?: boolean;
is_starred?: boolean;
last_editor?: LastEditor;
last_editor?: string;
lines?: number;
lines_more?: number;
linked_channel_id?: string;
Expand Down Expand Up @@ -133,7 +133,7 @@ export interface FilesMatch {
url_private?: string;
url_private_download?: string;
url_static_preview?: string;
user?: LastEditor;
user?: string;
user_team?: string;
username?: string;
}
Expand Down Expand Up @@ -659,12 +659,7 @@ export interface Cc {

export interface DmMpdmUsersWithFileAccess {
access?: string;
user_id?: LastEditor;
}

export enum LastEditor {
Empty = '',
U00000000 = 'U00000000',
user_id?: string;
}

export interface Favorite {
Expand Down Expand Up @@ -1408,7 +1403,7 @@ export interface Public {
reply_count?: number;
reply_users?: string[];
reply_users_count?: number;
share_user_id?: LastEditor;
share_user_id?: string;
source?: string;
team_id?: string;
ts?: string;
Expand Down Expand Up @@ -1553,7 +1548,7 @@ export interface MessagesMatch {
text?: string;
ts?: string;
type?: string;
user?: LastEditor;
user?: string;
username?: string;
}

Expand All @@ -1573,7 +1568,7 @@ export interface Channel {
name?: string;
name_normalized?: string;
pending_shared?: string[];
user?: LastEditor;
user?: string;
}

export interface Previous {
Expand All @@ -1584,7 +1579,7 @@ export interface Previous {
text?: string;
ts?: string;
type?: string;
user?: LastEditor;
user?: string;
username?: string;
}

Expand Down
33 changes: 32 additions & 1 deletion scripts/code_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,42 @@
# prerequisites
# npm install quicktype

require 'json'
require 'open3'

index_file = __dir__ + '/../packages/web-api/src/types/response/index.ts'
File.truncate(index_file, 0)

# The java-slack-sdk sample recorder masks each ID as "<letter>00000000" and each
# timestamp as "0000000000.000000", and writes map keys as "0000000000",
# "0000000000_" and so on. quicktype reads a placeholder repeated across a sample
# as an enum or a literal type, and only reads an object as a map when its keys
# are all digits, so these placeholders are blanked and the map keys renumbered.
module SampleNormalizer
PLACEHOLDER_VALUE = /\A(?:[A-Z]0{8}|0{10}\.0{6})\z/
PLACEHOLDER_KEY = /\A0{10}_*\z/

def self.normalize(node)
case node
when Hash
placeholder_keys = 0
node.each_with_object({}) do |(key, value), normalized|
if key.match?(PLACEHOLDER_KEY)
key = format('%010d', placeholder_keys)
placeholder_keys += 1
end
normalized[key] = normalize(value)
end
when Array
node.map { |value| normalize(value) }
when String
node.match?(PLACEHOLDER_VALUE) ? '' : node
else
node
end
end
end

class TsWriter

NOTICE = [
Expand Down Expand Up @@ -91,7 +122,7 @@ def append_multiple_classes_to_index(classes, class_file_name, index_file)

root_class_name << 'Response'
typedef_filepath = __dir__ + "/../packages/web-api/src/types/response/#{root_class_name}.ts"
input_json = json_file.read
input_json = JSON.generate(SampleNormalizer.normalize(JSON.parse(json_file.read)))
ts_writer.write(root_class_name, json_path, typedef_filepath, input_json)
ts_writer.append_to_index(root_class_name, index_file)
end
Expand Down