Created
January 22, 2021 09:40
-
-
Save SamWSoftware/156710f4db50798c7d7f30c9bcc8722d to your computer and use it in GitHub Desktop.
How spread works and limitations with nested objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const defaultRequest = { | |
| body: null, | |
| headers: {}, | |
| multiValueHeaders: {}, | |
| httpMethod: "", | |
| isBase64Encoded: false, | |
| path: '', | |
| pathParameters: null, | |
| queryStringParameters: null, | |
| multiValueQueryStringParameters: null, | |
| stageVariables: null, | |
| requestContext: { | |
| accountId: '', | |
| apiId: '', | |
| httpMethod: '', | |
| identity: { | |
| accessKey: '', | |
| accountId: '', | |
| apiKey: '', | |
| apiKeyId: '', | |
| caller: '', | |
| cognitoAuthenticationProvider: '', | |
| cognitoAuthenticationType: '', | |
| cognitoIdentityId: '', | |
| cognitoIdentityPoolId: '', | |
| principalOrgId: '', | |
| sourceIp: '', | |
| user: '', | |
| userAgent: '', | |
| userArn: '', | |
| }, | |
| path: '', | |
| stage: '', | |
| requestId: '', | |
| requestTimeEpoch: 3, | |
| resourceId: '', | |
| resourcePath: '', | |
| }, | |
| resource: '', | |
| }; | |
| const APIGatewayRequest = (option) => { | |
| return {...defaultRequest, ...option} | |
| } | |
| // Now if we want to set the path and httpMethod you might think you can do this: | |
| const request = APIGatewayRequest({path: '/user/1234', httpMethod: 'GET', requestContext: { path: '/user/1234', httpMethod: 'GET' }) | |
| console.log(request) | |
| // You will now get this | |
| /* | |
| { | |
| body: null, | |
| headers: {}, | |
| multiValueHeaders: {}, | |
| httpMethod: "GET", | |
| isBase64Encoded: false, | |
| path: '/user/1234', | |
| pathParameters: null, | |
| queryStringParameters: null, | |
| multiValueQueryStringParameters: null, | |
| stageVariables: null, | |
| requestContext: { | |
| path: '/user/1234', | |
| httpMethod: 'GET' | |
| }, | |
| resource: '', | |
| } | |
| */ | |
| // You've now lost all of the other details in the request context. In this case that ins't too bad as there | |
| // weren't any real values in the nested object. | |
| // If we do another example i can show how it can be an issue: | |
| const updateUser = (user, updateOptions) => { | |
| return {...user, ...updateOptions} | |
| } | |
| const originalUser = { | |
| name: 'Sam', | |
| age: 27, | |
| address: { | |
| firstLine: '123 My Road', | |
| city: 'manchester', | |
| country: 'UK', | |
| postcode: 'BL84HS' | |
| }, | |
| job: { | |
| employer: 'Complete Coding', | |
| wage: 12345, | |
| title: 'solution architect' | |
| }, | |
| hobbies: ['rock climbing', 'gaming', 'running'] | |
| } | |
| // if I want to now update my job title I could try this: | |
| const newUser = updateUser(originalUser, { job: {title: "Founder" } }); | |
| /* | |
| this will override the whole of the job object with just {title: "Founder" }, losing the employer and wage. | |
| You also have no way to update the hobbies without getting the hobbies, updating the array and passing the whole new array as an option | |
| Having nested object or array is one of the limitations of using the spread. | |
| There is a way to be able to just pass in two objects and merge them but it requires a lot more logic. | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment