-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathSLRESTContract.h
181 lines (161 loc) · 5.47 KB
/
SLRESTContract.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @file SLRESTContract.h
*
* @author Michael Schoonmaker
* @copyright (c) 2013 StrongLoop. All rights reserved.
*/
#import <Foundation/Foundation.h>
/** The verb SLRemoting uses when no verb has been specified by the server. */
extern NSString *SLRESTContractDefaultVerb;
/**
* A single item within a larger SLRESTContract, encapsulation a single route's
* verb and pattern, e.g. GET /widgets/:id.
*/
@interface SLRESTContractItem : NSObject
/** The pattern corresponding to this route, e.g. `/widgets/:id`. */
@property (readonly, nonatomic, copy) NSString *pattern;
/** The verb corresponding to this route, e.g. `GET`. */
@property (readonly, nonatomic, copy) NSString *verb;
/** Indication that this item is a multipart form mime type. */
@property (readonly, nonatomic, assign) BOOL multipart;
/**
* Returns a new item encapsulating the given pattern.
*
* @param pattern The pattern to represent.
* @return A new item.
*/
+ (instancetype)itemWithPattern:(NSString *)pattern;
/**
* Returns a new item encapsulating the given pattern and verb.
*
* @param pattern The pattern to represent.
* @param verb The verb to represent.
* @return A new item.
*/
+ (instancetype)itemWithPattern:(NSString *)pattern verb:(NSString *)verb;
/**
* Returns a new item encapsulating the given pattern, verb and multipart setting.
*
* @param pattern The pattern to represent.
* @param verb The verb to represent.
* @param multiplart Indicates this item is a multipart mime type.
* @return A new item.
*/
+ (instancetype)itemWithPattern:(NSString *)pattern verb:(NSString *)verb multipart:(BOOL)multipart;
@end
/**
* A contract specifies how remote method names map to HTTP routes.
*
* For example, if a remote method on the server has been remapped like so:
*
* @code{.js}
* project.getObject = function (id, callback) {
* callback(null, { ... });
* };
* helper.method(project.getObject, {
* http: { verb: 'GET', path: '/:id'},
* accepts: { name: 'id', type: 'string' }
* returns: { name: 'object', type: 'object' }
* })
* @endcode
*
* The new route is GET /:id, instead of POST /project/getObject, so we
* need to update our contract on the client:
*
* @code{.m}
* [contract addItem:[SLRESTContractItem itemWithPattern:@"/:id" verb:@"GET"]
* forMethod:@"project.getObject"];
* @endcode
*/
@interface SLRESTContract : NSObject
/**
* A read-only representation of the internal contract. Used for
* SLRESTContract::addItemsFromContract:.
*/
@property (readonly, nonatomic) NSDictionary *dict;
/**
* Returns a new, empty contract.
*
* @return A new, empty contract.
*/
+ (instancetype)contract;
/**
* Adds a single item to this contract. The item can be shared among different
* contracts, managed by the sum of all contracts that contain it. Similarly,
* each item can be used for more than one method, like so:
*
* @code{.m}
* SLRESTContractItem *upsert = [SLRESTContractItem itemWithPattern:@"/widgets/:id"
* andVerb:@"PUT"];
* [contract addItem:upsert forMethod:@"widgets.create"];
* [contract addItem:upsert forMethod:@"widgets.update"];
* @endcode
*
* @param item The item to add to this contract.
* @param method The method the item should represent.
*/
- (void)addItem:(SLRESTContractItem *)item forMethod:(NSString *)method;
/**
* Adds all items from contract.
*
* @see addItem:forMethod:
*
* @param contract The contract to copy from.
*/
- (void)addItemsFromContract:(SLRESTContract *)contract;
/**
* Resolves a specific method, replacing pattern fragments with the optional
* `parameters` as appropriate.
* Also removes consumed key-value pairs from the mutable `parameters`.
*
* @param method The method to resolve.
* @param parameters Pattern parameters. Can be `nil`.
* @return The complete, resolved URL.
*/
- (NSString *)urlForMethod:(NSString *)method
parameters:(NSMutableDictionary *)parameters;
/**
* Returns the HTTP verb for the given method string.
*
* @param method The method to resolve.
* @return The resolved verb.
*/
- (NSString *)verbForMethod:(NSString *)method;
/**
* Returns the multipart setting for the given method string.
*
* @param method The method to resolve.
* @return The mutipart setting.
*/
- (BOOL)multipartForMethod:(NSString *)method;
/**
* Generates a fallback URL for a method whose contract has not been customized.
*
* @param method The method to generate from.
* @return The resolved URL.
*/
- (NSString *)urlForMethodWithoutItem:(NSString *)method;
/**
* Returns the custom pattern representing the given method string, or `nil` if
* no custom pattern exists.
*
* @param method The method to resolve.
* @return The custom pattern if one exists, `nil` otherwise.
*/
- (NSString *)patternForMethod:(NSString *)method;
/**
* Returns a rendered URL pattern using the parameters provided.
* Also removes consumed key-value pairs from the mutable `parameters`.
* For example,
* `@"/widgets/:id"` + `@{ @"id": @"57", @"price": @"42.00" }` begets
* `@"/widgets/57"`.
* And `parameter` becomes `@{ @"price": @"42.00" }`
*
* @param pattern The pattern to render.
* @param parameters Values to render with.
* This is mutable and consumed key-value pairs get removed.
* @return The rendered URL.
*/
- (NSString *)urlWithPattern:(NSString *)pattern
parameters:(NSMutableDictionary *)parameters;
@end