2020-11-03 10:49:48 +08:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import type {JSONValue, ResponseBase} from 'react-client/src/ReactFlightClient';
|
|
|
|
|
|
|
|
|
|
import type JSResourceReference from 'JSResourceReference';
|
|
|
|
|
|
|
|
|
|
export type ModuleReference<T> = JSResourceReference<T>;
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
parseModelString,
|
|
|
|
|
parseModelTuple,
|
|
|
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
resolveModuleReference,
|
|
|
|
|
preloadModule,
|
|
|
|
|
requireModule,
|
|
|
|
|
} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
|
|
2021-04-07 22:57:43 +08:00
|
|
|
import isArray from 'shared/isArray';
|
|
|
|
|
|
2020-11-03 10:49:48 +08:00
|
|
|
export type {ModuleMetaData} from 'ReactFlightNativeRelayClientIntegration';
|
|
|
|
|
|
2020-11-11 11:54:42 +08:00
|
|
|
export type UninitializedModel = JSONValue;
|
2020-11-03 10:49:48 +08:00
|
|
|
|
|
|
|
|
export type Response = ResponseBase;
|
|
|
|
|
|
|
|
|
|
function parseModelRecursively(response: Response, parentObj, value) {
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
return parseModelString(response, parentObj, value);
|
|
|
|
|
}
|
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
2021-04-07 22:57:43 +08:00
|
|
|
if (isArray(value)) {
|
2020-12-17 03:53:51 +08:00
|
|
|
const parsedValue = [];
|
2020-11-03 10:49:48 +08:00
|
|
|
for (let i = 0; i < value.length; i++) {
|
2020-12-17 03:53:51 +08:00
|
|
|
(parsedValue: any)[i] = parseModelRecursively(
|
|
|
|
|
response,
|
|
|
|
|
value,
|
|
|
|
|
value[i],
|
|
|
|
|
);
|
2020-11-03 10:49:48 +08:00
|
|
|
}
|
2020-12-17 03:53:51 +08:00
|
|
|
return parseModelTuple(response, parsedValue);
|
2020-11-03 10:49:48 +08:00
|
|
|
} else {
|
2020-12-17 03:53:51 +08:00
|
|
|
const parsedValue = {};
|
2020-11-03 10:49:48 +08:00
|
|
|
for (const innerKey in value) {
|
2020-12-17 03:53:51 +08:00
|
|
|
(parsedValue: any)[innerKey] = parseModelRecursively(
|
2020-11-03 10:49:48 +08:00
|
|
|
response,
|
|
|
|
|
value,
|
|
|
|
|
value[innerKey],
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-12-17 03:53:51 +08:00
|
|
|
return parsedValue;
|
2020-11-03 10:49:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dummy = {};
|
|
|
|
|
|
|
|
|
|
export function parseModel<T>(response: Response, json: UninitializedModel): T {
|
|
|
|
|
return (parseModelRecursively(response, dummy, json): any);
|
|
|
|
|
}
|