Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
npm
common
Commits
6369d976
Commit
6369d976
authored
Nov 09, 2018
by
Vladislav Lagunov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Исправление ошибок
parent
2f06a7e2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
62 deletions
+75
-62
decoder/index.ts
+0
-0
eff/index.ts
+13
-5
either/index.ts
+1
-2
http/index.ts
+57
-51
maybe/index.ts
+4
-4
No files found.
decoder/index.ts
View file @
6369d976
This diff is collapsed.
Click to expand it.
eff.ts
→
eff
/index
.ts
View file @
6369d976
import
*
as
either
from
'./either'
;
import
{
Either
}
from
'./either'
;
import
{
absurd
}
from
'.
/internal
'
;
import
*
as
either
from
'.
.
/either'
;
import
{
Either
}
from
'.
.
/either'
;
import
{
absurd
}
from
'.
./types
'
;
// Alias
...
...
@@ -66,6 +66,14 @@ export class EffBase<Error, Success> {
return
new
Chain
(
this
.
toEff
(),
andThen
);
}
run
(
onNext
:
(
x
:
Either
<
Error
,
Success
>
)
=>
void
,
onComplete
:
()
=>
void
):
()
=>
void
{
return
go
(
this
.
toEff
(),
onNext
,
onComplete
);
}
subscribe
(
onNext
:
(
x
:
Either
<
Error
,
Success
>
)
=>
void
,
onComplete
:
()
=>
void
):
()
=>
void
{
return
go
(
this
.
toEff
(),
onNext
,
onComplete
);
}
toEff
()
{
return
this
as
any
as
Eff
<
Error
,
Success
>
;
}
...
...
@@ -197,7 +205,7 @@ export function go<Error, Success>(effect: Eff<Error, Success>, onNext: (x: Eith
}
if
(
effect
instanceof
Thunk
)
{
onNext
(
effect
.
run
.
apply
(
_this
,
effect
.
args
));
onNext
(
effect
.
_
run
.
apply
(
_this
,
effect
.
args
));
onComplete
();
return
noopFunc
;
}
...
...
@@ -295,7 +303,7 @@ export class Pure<Error, Success> extends EffBase<Error, Success> {
export
class
Thunk
<
Error
,
Success
>
extends
EffBase
<
Error
,
Success
>
{
constructor
(
readonly
run
:
(...
args
:
unknown
[])
=>
Either
<
Error
,
Success
>
,
readonly
_
run
:
(...
args
:
unknown
[])
=>
Either
<
Error
,
Success
>
,
readonly
args
:
unknown
[],
)
{
super
();
}
}
...
...
either/index.ts
View file @
6369d976
import
{
Expr
}
from
'
~
/types'
;
import
{
Expr
}
from
'
..
/types'
;
/** convenient instance methods for `Either` */
...
...
@@ -183,7 +183,6 @@ export function traverse<L,A,B>(arr: Array<unknown>, f?: Function): Either<L,unk
}
export
const
Either
=
{
Left
,
Right
,
failure
,
success
,
left
,
of
,
ap
,
traverse
,
};
http.ts
→
http
/index
.ts
View file @
6369d976
import
{
HasEffect
,
Subscribe
,
Eff
}
from
'./eff'
;
import
*
as
eff
from
'./eff'
;
import
{
Decoder
,
Problem
}
from
'.
/decode
'
;
import
{
Either
}
from
'./either'
;
import
*
as
either
from
'./either'
;
import
{
HasEffect
,
Subscribe
,
Eff
}
from
'.
.
/eff'
;
import
*
as
eff
from
'.
.
/eff'
;
import
{
Decoder
,
Problem
}
from
'.
./decoder
'
;
import
{
Either
}
from
'.
.
/either'
;
import
*
as
either
from
'.
.
/either'
;
/** http method */
...
...
@@ -24,7 +24,7 @@ export interface RequestProgress extends Request {
}
/**
r
aw error */
/**
R
aw error */
export
type
HttpError
=
|
{
tag
:
'BadUrl'
,
desc
:
string
}
|
{
tag
:
'BadPayload'
,
desc
:
string
}
...
...
@@ -34,7 +34,7 @@ export type HttpError =
|
{
tag
:
'NetworkError'
}
/**
r
esponce */
/**
R
esponce */
export
interface
Response
{
url
:
string
;
status
:
number
;
...
...
@@ -44,12 +44,12 @@ export interface Response {
}
/**
q
uery params */
/**
Q
uery params */
export
type
ParamsPrimitive
=
number
|
string
|
undefined
|
null
;
export
type
Params
=
Record
<
string
,
ParamsPrimitive
|
ParamsPrimitive
[]
>
;
/**
p
rogress */
/**
P
rogress */
export
type
Progress
=
|
{
tag
:
'Computable'
,
total
:
number
,
loaded
:
number
}
|
{
tag
:
'Uncomputable'
}
...
...
@@ -58,49 +58,10 @@ export type Progress =
export
class
HttpEffect
<
A
>
extends
HasEffect
<
HttpError
,
A
>
{
constructor
(
readonly
request
:
Request
|
RequestProgress
,
)
{
super
();
};
toEffect
()
{
return
new
Subscribe
<
HttpError
,
any
>
((
onNext
,
onComplete
)
=>
{
const
req
=
this
.
request
;
const
onSuccess
=
(
x
:
Response
)
=>
(
'progress'
in
req
&&
req
.
progress
?
onNext
(
either
.
right
(
either
.
right
(
x
)))
:
onNext
(
either
.
right
(
x
)),
onComplete
());
const
onProgress
=
(
x
:
Progress
)
=>
'progress'
in
req
&&
req
.
progress
?
onNext
(
either
.
right
(
either
.
left
(
x
)))
:
void
0
;
const
onFailure
=
(
x
:
HttpError
)
=>
(
onNext
(
either
.
left
(
x
)),
onComplete
());
const
xhr
=
new
XMLHttpRequest
();
xhr
.
addEventListener
(
'error'
,
()
=>
onFailure
({
tag
:
'NetworkError'
}));
xhr
.
addEventListener
(
'timeout'
,
()
=>
onFailure
({
tag
:
'Timeout'
}));
xhr
.
addEventListener
(
'load'
,
()
=>
onSuccess
({
url
:
xhr
.
responseURL
,
status
:
xhr
.
status
,
statusText
:
xhr
.
statusText
,
headers
:
parseHeaders
(
xhr
.
getAllResponseHeaders
()),
body
:
xhr
.
response
||
xhr
.
responseText
,
}));
try
{
xhr
.
open
(
req
.
method
,
req
.
url
,
true
);
}
catch
(
e
)
{
onFailure
({
tag
:
'BadUrl'
,
desc
:
req
.
url
});
}
if
(
'progress'
in
req
&&
req
.
progress
)
{
xhr
.
addEventListener
(
'progress'
,
e
=>
onProgress
(
e
.
lengthComputable
?
{
tag
:
'Computable'
,
loaded
:
e
.
loaded
,
total
:
e
.
total
}
:
{
tag
:
'Uncomputable'
}));
}
if
(
req
.
timeout
)
xhr
.
timeout
=
req
.
timeout
;
if
(
typeof
(
req
.
withCredentials
)
!==
'undefined'
)
xhr
.
withCredentials
=
req
.
withCredentials
;
if
(
typeof
(
req
.
headers
)
!==
'undefined'
)
{
for
(
let
key
in
req
.
headers
)
{
if
(
!
req
.
headers
.
hasOwnProperty
(
key
))
continue
;
const
value
=
req
.
headers
[
key
];
if
(
typeof
(
value
)
!==
'undefined'
&&
value
!==
null
)
xhr
.
setRequestHeader
(
key
,
String
(
value
));
}
}
const
body
=
Object
.
prototype
.
toString
.
apply
(
req
.
body
)
===
'[object Object]'
?
JSON
.
stringify
(
req
.
body
)
:
req
.
body
;
xhr
.
send
(
body
);
return
()
=>
xhr
.
abort
();
});
return
new
Subscribe
<
HttpError
,
any
>
((
onNext
,
onComplete
)
=>
doXHR
(
this
.
request
,
onNext
,
onComplete
));
}
}
...
...
@@ -129,6 +90,51 @@ export function post(url: string, request?: Omit<Request|RequestProgress, 'url'|
}
/**
* Реализания запроса
*/
export
function
doXHR
(
req
:
Request
,
onNext
:
(
x
:
Either
<
HttpError
,
Response
>
)
=>
void
,
onComplete
:
()
=>
void
):
()
=>
void
;
export
function
doXHR
(
req
:
RequestProgress
,
onNext
:
(
x
:
Either
<
HttpError
,
Either
<
Progress
,
Response
>>
)
=>
void
,
onComplete
:
()
=>
void
):
()
=>
void
;
export
function
doXHR
(
req
:
Request
|
RequestProgress
,
onNext
:
(
x
:
Either
<
HttpError
,
any
>
)
=>
void
,
onComplete
:
()
=>
void
):
()
=>
void
{
const
onSuccess
=
(
x
:
Response
)
=>
(
'progress'
in
req
&&
req
.
progress
?
onNext
(
either
.
right
(
either
.
right
(
x
)))
:
onNext
(
either
.
right
(
x
)),
onComplete
());
const
onProgress
=
(
x
:
Progress
)
=>
'progress'
in
req
&&
req
.
progress
?
onNext
(
either
.
right
(
either
.
left
(
x
)))
:
void
0
;
const
onFailure
=
(
x
:
HttpError
)
=>
(
onNext
(
either
.
left
(
x
)),
onComplete
());
const
xhr
=
new
XMLHttpRequest
();
xhr
.
addEventListener
(
'error'
,
()
=>
onFailure
({
tag
:
'NetworkError'
}));
xhr
.
addEventListener
(
'timeout'
,
()
=>
onFailure
({
tag
:
'Timeout'
}));
xhr
.
addEventListener
(
'load'
,
()
=>
onSuccess
({
url
:
xhr
.
responseURL
,
status
:
xhr
.
status
,
statusText
:
xhr
.
statusText
,
headers
:
parseHeaders
(
xhr
.
getAllResponseHeaders
()),
body
:
xhr
.
response
||
xhr
.
responseText
,
}));
try
{
xhr
.
open
(
req
.
method
,
req
.
url
,
true
);
}
catch
(
e
)
{
onFailure
({
tag
:
'BadUrl'
,
desc
:
req
.
url
});
}
if
(
'progress'
in
req
&&
req
.
progress
)
{
xhr
.
addEventListener
(
'progress'
,
e
=>
onProgress
(
e
.
lengthComputable
?
{
tag
:
'Computable'
,
loaded
:
e
.
loaded
,
total
:
e
.
total
}
:
{
tag
:
'Uncomputable'
}));
}
if
(
req
.
timeout
)
xhr
.
timeout
=
req
.
timeout
;
if
(
typeof
(
req
.
withCredentials
)
!==
'undefined'
)
xhr
.
withCredentials
=
req
.
withCredentials
;
if
(
typeof
(
req
.
headers
)
!==
'undefined'
)
{
for
(
let
key
in
req
.
headers
)
{
if
(
!
req
.
headers
.
hasOwnProperty
(
key
))
continue
;
const
value
=
req
.
headers
[
key
];
if
(
typeof
(
value
)
!==
'undefined'
&&
value
!==
null
)
xhr
.
setRequestHeader
(
key
,
String
(
value
));
}
}
const
body
=
Object
.
prototype
.
toString
.
apply
(
req
.
body
)
===
'[object Object]'
?
JSON
.
stringify
(
req
.
body
)
:
req
.
body
;
xhr
.
send
(
body
);
return
()
=>
xhr
.
abort
();
}
/** parse response as JSON */
export
function
expectJSON
<
A
>
(
decoder
:
Decoder
<
A
>
):
(
resp
:
Response
)
=>
Eff
<
HttpError
,
A
>
{
return
resp
=>
{
...
...
@@ -146,7 +152,7 @@ export function expectJSON<A>(decoder: Decoder<A>): (resp: Response) => Eff<Http
}
/**
p
arse headers from string to dict */
/**
P
arse headers from string to dict */
function
parseHeaders
(
rawHeaders
:
string
):
Record
<
string
,
string
>
{
const
output
=
{};
const
lines
=
rawHeaders
.
split
(
'
\
r
\
n'
);
...
...
@@ -161,7 +167,7 @@ function parseHeaders(rawHeaders: string): Record<string, string> {
}
/**
b
uild an url */
/**
B
uild an url */
export
function
join
(...
args
:
Array
<
string
|
Params
>
):
string
{
let
path
=
''
;
let
params
=
{}
as
Record
<
string
,
string
>
;
...
...
option
.ts
→
maybe/index
.ts
View file @
6369d976
import
{
Expr
}
from
'.
/internal/expr
'
;
import
{
Expr
}
from
'.
./types
'
;
/** adt */
...
...
@@ -7,7 +7,7 @@ export type Maybe<A> = Option<A>
/** instance method for convenience */
export
class
Option
Base
<
A
>
{
export
class
Maybe
Base
<
A
>
{
readonly
_A
:
A
;
/** map */
...
...
@@ -63,14 +63,14 @@ export class OptionBase<A> {
/** empty container */
export
class
None
<
A
>
extends
Option
Base
<
A
>
{
export
class
None
<
A
>
extends
Maybe
Base
<
A
>
{
readonly
_a
:
A
;
readonly
tag
:
'None'
=
'None'
;
}
/** container with a value */
export
class
Some
<
A
>
extends
Option
Base
<
A
>
{
export
class
Some
<
A
>
extends
Maybe
Base
<
A
>
{
readonly
_a
:
A
;
readonly
tag
:
'Some'
=
'Some'
;
constructor
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment