-
Notifications
You must be signed in to change notification settings - Fork 1
Make protocol agnostic to usage #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
markmur
wants to merge
1
commit into
main
Choose a base branch
from
agnostic-protocol-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+824
−500
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
85 changes: 75 additions & 10 deletions
85
platforms/react-native/modules/@shopify/checkout-kit-react-native/src/protocol.ts
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
1 change: 1 addition & 0 deletions
1
...ft/Samples/CheckoutKitSwiftDemo/CheckoutKitSwiftDemo/Sources/CheckoutProtocolClient.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import SafariServices | ||
| import ShopifyCheckoutKit | ||
| import ShopifyCheckoutProtocol | ||
| import UIKit | ||
|
|
||
|
|
||
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
128 changes: 128 additions & 0 deletions
128
platforms/swift/Sources/ShopifyCheckoutKit/CheckoutProtocol.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| #if !COCOAPODS | ||
| import ShopifyCheckoutProtocol | ||
| #endif | ||
| import Foundation | ||
|
|
||
| public enum CheckoutProtocol { | ||
| public typealias Client = CheckoutTransport.Client | ||
|
|
||
| public static func url(for url: URL, delegations: [String] = []) -> URL { | ||
| CheckoutTransport.url(for: url, delegations: delegations) | ||
| } | ||
|
|
||
| public static let defaultDelegations: [String] = ["window.open"] | ||
|
|
||
| static let methodNotFoundCode = -32601 | ||
| static let methodNotFoundMessage = "Method not found" | ||
|
|
||
| public static let complete = GeneratedProtocolCatalog.ecComplete | ||
| public static let error = GeneratedProtocolCatalog.ecError | ||
| public static let lineItemsChange = GeneratedProtocolCatalog.ecLineItemsChange | ||
| public static let messagesChange = GeneratedProtocolCatalog.ecMessagesChange | ||
| public static let start = GeneratedProtocolCatalog.ecStart | ||
| public static let totalsChange = GeneratedProtocolCatalog.ecTotalsChange | ||
|
|
||
| static let supportedProtocolMethods: Set<String> = [ | ||
| CheckoutTransport.readyMethod, | ||
| start.method, | ||
| complete.method, | ||
| error.method, | ||
| lineItemsChange.method, | ||
| messagesChange.method, | ||
| totalsChange.method, | ||
| windowOpen.method | ||
| ] | ||
|
|
||
| static func supportedProtocolMethod(_ message: String) -> String? { | ||
| guard | ||
| let envelope = try? JSONDecoder().decode(MethodEnvelope.self, from: Data(message.utf8)), | ||
| envelope.jsonrpc == "2.0", | ||
| supportedProtocolMethods.contains(envelope.method) | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| return envelope.method | ||
| } | ||
|
|
||
| static func methodNotFoundResponse(forUnsupportedProtocolRequest message: String) -> String? { | ||
| guard | ||
| let request = try? JSONDecoder().decode(RequestEnvelope.self, from: Data(message.utf8)), | ||
| request.jsonrpc == "2.0", | ||
| !supportedProtocolMethods.contains(request.method), | ||
| let id = request.id | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| let response = MethodNotFoundResponse( | ||
| id: id, | ||
| error: MethodNotFoundError(code: methodNotFoundCode, message: methodNotFoundMessage) | ||
| ) | ||
| let encoder = JSONEncoder() | ||
| encoder.outputFormatting = [.sortedKeys] | ||
| guard let data = try? encoder.encode(response) else { return nil } | ||
| return String(data: data, encoding: .utf8) | ||
| } | ||
| } | ||
|
|
||
| private struct MethodEnvelope: Decodable { | ||
| let jsonrpc: String | ||
| let method: String | ||
| } | ||
|
|
||
| private struct RequestEnvelope: Decodable { | ||
| let jsonrpc: String | ||
| let method: String | ||
| let id: RequestID? | ||
| } | ||
|
|
||
| enum RequestID: Codable, Equatable { | ||
| case string(String) | ||
| case int(Int64) | ||
| case null | ||
|
|
||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.singleValueContainer() | ||
|
|
||
| if container.decodeNil() { | ||
| self = .null | ||
| } else if let value = try? container.decode(String.self) { | ||
| self = .string(value) | ||
| } else if let value = try? container.decode(Int64.self) { | ||
| self = .int(value) | ||
| } else { | ||
| throw DecodingError.typeMismatch( | ||
| RequestID.self, | ||
| DecodingError.Context( | ||
| codingPath: decoder.codingPath, | ||
| debugDescription: "JSON-RPC id must be a string, integer, or null" | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.singleValueContainer() | ||
|
|
||
| switch self { | ||
| case let .string(value): | ||
| try container.encode(value) | ||
| case let .int(value): | ||
| try container.encode(value) | ||
| case .null: | ||
| try container.encodeNil() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct MethodNotFoundResponse: Encodable { | ||
| let jsonrpc = "2.0" | ||
| let id: RequestID | ||
| let error: MethodNotFoundError | ||
| } | ||
|
|
||
| private struct MethodNotFoundError: Encodable { | ||
| let code: Int | ||
| let message: String | ||
| } |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: figure out if we can remove this mapping