Skip to content

Commit 6d14c12

Browse files
chrisbobbegnprice
authored andcommitted
api types: Shorten name of "cooked" response to ServerSettings
The type for the raw, unprocessed payload from the server is now just named ApiResponseServerSettings, from ServerApiResponseServerSettings. The type for the "cooked" payload, with our validation on `realm_name`, now has the shorter name ServerSettings, from ApiResponseServerSettings. The validation is done in a new helper function, `transform`. We'll add more stuff there soon, and change the ServerSettings type to be a more "cooked" form of the data.
1 parent c13b15a commit 6d14c12

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

src/account/AccountPickScreen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Screen from '../common/Screen';
1717
import ViewPlaceholder from '../common/ViewPlaceholder';
1818
import AccountList from './AccountList';
1919
import { accountSwitch, removeAccount } from '../actions';
20-
import type { ApiResponseServerSettings } from '../api/settings/getServerSettings';
20+
import type { ServerSettings } from '../api/settings/getServerSettings';
2121
import { showConfirmationDialog, showErrorAlert } from '../utils/info';
2222
import { tryStopNotifications } from '../notification/notifTokens';
2323

@@ -46,7 +46,7 @@ export default function AccountPickScreen(props: Props): Node {
4646
});
4747
} else {
4848
try {
49-
const serverSettings: ApiResponseServerSettings = await api.getServerSettings(realm);
49+
const serverSettings: ServerSettings = await api.getServerSettings(realm);
5050
navigation.push('auth', { serverSettings });
5151
} catch {
5252
// TODO: show specific error message from error object

src/api/settings/getServerSettings.js

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export type ExternalAuthenticationMethod = {|
2525

2626
/** https://zulip.com/api/get-server-settings */
2727
// Current to FL 121.
28-
export type ApiResponseServerSettings = {|
28+
type ApiResponseServerSettings = {|
2929
...$Exact<ApiResponseSuccess>,
3030
authentication_methods: AuthenticationMethods,
3131

@@ -45,16 +45,6 @@ export type ApiResponseServerSettings = {|
4545
email_auth_enabled: boolean,
4646
require_email_format_usernames: boolean,
4747
realm_uri: string,
48-
realm_name: string,
49-
realm_icon: string,
50-
realm_description: string,
51-
52-
// TODO(server-5.0): New in FL 116.
53-
realm_web_public_access_enabled?: boolean,
54-
|};
55-
56-
type ServerApiResponseServerSettings = {|
57-
...ApiResponseServerSettings,
5848

5949
// When missing, the user requested the root domain of a Zulip server, but
6050
// there is no realm there. User error.
@@ -71,18 +61,26 @@ type ServerApiResponseServerSettings = {|
7161
// TODO(server-future): Then, when all supported servers give that error,
7262
// make this property required.
7363
realm_name?: string,
64+
65+
realm_icon: string,
66+
realm_description: string,
67+
68+
// TODO(server-5.0): New in FL 116.
69+
realm_web_public_access_enabled?: boolean,
7470
|};
7571

76-
/** See https://zulip.com/api/get-server-settings */
77-
export default async (realm: URL): Promise<ApiResponseServerSettings> => {
78-
const result: ServerApiResponseServerSettings = await apiGet(
79-
{ apiKey: '', email: '', realm },
80-
'server_settings',
81-
);
72+
export type ServerSettings = $ReadOnly<{|
73+
...ApiResponseServerSettings,
74+
+realm_name: string,
75+
|}>;
8276

83-
const { realm_name } = result;
77+
/**
78+
* Make a ServerSettings from a raw API response.
79+
*/
80+
function transform(raw: ApiResponseServerSettings): ServerSettings {
81+
const { realm_name } = raw;
8482
if (realm_name == null) {
85-
// See comment on realm_name in ServerApiResponseServerSettings.
83+
// See comment on realm_name in ApiResponseServerSettings.
8684
//
8785
// This error copies the proper error that servers *sometimes* give when
8886
// the root domain is requested and there's no realm there. [1]
@@ -95,5 +93,18 @@ export default async (realm: URL): Promise<ApiResponseServerSettings> => {
9593
throw new ApiError(400, { code: 'BAD_REQUEST', msg: 'Subdomain required', result: 'error' });
9694
}
9795

98-
return { ...result, realm_name };
96+
return {
97+
...raw,
98+
realm_name,
99+
};
100+
}
101+
102+
/** See https://zulip.com/api/get-server-settings */
103+
export default async (realm: URL): Promise<ServerSettings> => {
104+
const result: ApiResponseServerSettings = await apiGet(
105+
{ apiKey: '', email: '', realm },
106+
'server_settings',
107+
);
108+
109+
return transform(result);
99110
};

src/start/AuthScreen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AppleAuthenticationCredential } from 'expo-apple-authentication';
77
import * as AppleAuthentication from 'expo-apple-authentication';
88

99
import type {
10-
ApiResponseServerSettings,
10+
ServerSettings,
1111
AuthenticationMethods,
1212
ExternalAuthenticationMethod,
1313
} from '../api/settings/getServerSettings';
@@ -173,7 +173,7 @@ export const activeAuthentications = (
173173
type OuterProps = $ReadOnly<{|
174174
// These should be passed from React Navigation
175175
navigation: AppNavigationProp<'auth'>,
176-
route: RouteProp<'auth', {| serverSettings: ApiResponseServerSettings |}>,
176+
route: RouteProp<'auth', {| serverSettings: ServerSettings |}>,
177177
|}>;
178178

179179
type SelectorProps = $ReadOnly<{|

src/start/RealmInputScreen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useFocusEffect } from '@react-navigation/native';
66

77
import type { RouteProp } from '../react-navigation';
88
import type { AppNavigationProp } from '../nav/AppNavigator';
9-
import type { ApiResponseServerSettings } from '../api/settings/getServerSettings';
9+
import type { ServerSettings } from '../api/settings/getServerSettings';
1010
import ErrorMsg from '../common/ErrorMsg';
1111
import ZulipTextIntl from '../common/ZulipTextIntl';
1212
import Screen from '../common/Screen';
@@ -72,7 +72,7 @@ export default function RealmInputScreen(props: Props): Node {
7272
setProgress(true);
7373
setError(null);
7474
try {
75-
const serverSettings: ApiResponseServerSettings = await api.getServerSettings(parsedRealm);
75+
const serverSettings: ServerSettings = await api.getServerSettings(parsedRealm);
7676
navigation.push('auth', { serverSettings });
7777
Keyboard.dismiss();
7878
} catch (errorIllTyped) {

0 commit comments

Comments
 (0)